Topic: Judging Statement Looping statements
I. Judgment statement 1, single branch
1 if (condition) {23// Insert code here, indicating if the condition in the execution of statement 4 }
Attention:
1. The condition must be Boolean
2.if with only one line of code, you can omit {}
1 int b=3,c=4,d=5; 2 if (b<c) 3 if (b>D) 4 System.out.println (d); 5 Else 6 System.out.println (c);
Output is 4
2. Dual Branch
1 if (condition) {2 // condition set up output 3 }else{4// condition not established output 5 }
Attention:
1.else can not bring conditions;
2.if,else with only one line of code, you can omit {}
3. Multi-Branch if
1 if (condition) {23 } Else if (condition) {4 can have more than 5 }else{6 Can omit 7 }
Attention:
1.if,else if can be conditional
2.if,else If,else with only one statement, you can omit {}
4, multi-branch switch
1 Switch(expression) {2 CaseConstant value://This label can have multiple3 code block;4 Break;5 6 default://can omit7 code block:8 Break;//can omit9 Ten}
Attention:
1. The value type of the expression matches the constant value type in the label;
2. The expression type must be Byte,short,int,char,string,enum
3. Labels cannot be the same;
4. Support Tag stacking.
The difference between multiple if and switch:
- Switch can only be judged by the equivalent condition, and multiple if any judgment can be used (more for interval judgment)
- Equivalent judgment switch efficiency is high.
Second, the circular statement
Fixed loop for
Non-stationary while Do-while
Number of fixed cycles
1 // an initial value; a terminating value; an iteration. 2 for (int i=0;i<5;i++) {3 // Loop Body 4 }
First condition judgment, after loop operation.
Non-fixed number of cycles:
1 while (condition) {2 loop body;// cyclic operation 3 }
First condition judgment, after loop operation.
1 Do {2 loop body; 3 4 }while (condition);
Execute first, then judge.
Java Basic Three