Java basics 3-basic statements and java3-statements

Source: Internet
Author: User

Java basics 3-basic statements and java3-statements

1.Condition StatementA conditional statement is a selective code execution.

Including the if and switch condition statements.

If Condition Statement:

Syntax:

(1) if (Condition Statement ){

// Code to be executed when the condition is set

}

(2) if (Condition Statement ){

} Else {

}

(3) if (Condition Statement ){

} Else if (){

}....

(4) if (){

} Else if (){

} Else {

}

Note:

Else if and else cannot appear separately. They must match if !!!

 

Switch Condition Statement, Syntax:

Switch (expression ){

Case Constant Value: Operation statement

Break;

Case Constant Value: Operation statement

Break;

}

In the switch Condition Statement, the expression value can only be byte, short, char, int, but in JDK, you can use String.

2. Arithmetic Operators

+,-, *,/, %, ++ ,--

++ And -- features:

++ Adds 1 before output, ++ adds 1 after output

-- First subtract 1 and then output, -- then output and then subtract 1

3. Value assignment operator

Is to assign the value to a variable.

=, + =,-=, * =,/=. % =

+ = Indicates that two variables are added and assigned to another variable.

For example: int num1 = 10; int num2 = 20; num1 + = num2; it is equivalent to num1 = num1 + num2;

 4. Comparison Operators

>,<, >=, <=, == ,! =

5. logical operators

Logical operators are used to calculate the expressions of boolean results. The calculation results are always boolean.

&:

|: Or

^: Exclusive or description: When the expressions on both sides are consistent, the output result is false; otherwise, the output result is true.

! : The opposite result.

&: Short Circuit and

If the first condition is false, the result is directly output.

|: Short circuit or

If the first condition is true, the result is directly output.

6. + join Operators

Any data type connected by a plus sign (+) is a new string.

7. Three-object Operator

Syntax:

X? Y: Z

A result of the X expression must be of the boolean type. If the value of X is true, the value of Y is output; otherwise, the value of Z is output.

8. Expression

The result obtained by performing operations on the operands in the expression is called the value of the expression. The data type of the expression value is the expression type.

9. Loop statements

(1). for Loop

(2). while Loop

(3). do/while LOOP

For Loop Syntax:

For (initialization variable; cycle condition; changing the cycle condition ){

}

While loop Syntax:

While (loop condition ){

Change cycle conditions

}

Do/while LOOP Syntax:

Do {

Change cycle conditions

} While (Cyclic condition );

Note: do/while will be executed at least once no matter whether the condition is true or not.

10. break and conitune

  • The break statement is used to terminate the execution of a statement block.
  • The continue statement is used to skip one execution of a loop statement block.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.