Process control
- Conditional flow control: if..else, switch
- Loop flow control: while, do...while, for
Conditional flow control
if..else
grammar
if(布尔表达式){
//如果为true执行代码
}[else if(布尔表达式){
//....
}]...
[else{
//...
}]
Note
1. Boolean expressions in java can only be true/false
2. If there is only one curly brace in the execution body, it can be omitted
3. It is recommended to indent when writing if..else
switch
grammar
switch(表达式){
case 值1:[执行体;break;]
...
case 值n:[执行体n;break;]
[default:执行体;]
}
Note
1. Expressions can only be byte/short/int/char/boolean, JDK5.0 extends enumeration types, JDK7.0 extends String.
Note: long type is not allowed
2. The judgment in the switch statement will only be executed Once, if the judgment is established, no further judgment will be made, usually with the break keyword to achieve the function
loop flow control
while,do…while,for
while loop
grammar
while(布尔表达式){
//循环体
}
Note : Usually we need to provide loop end condition and step for loop control statement
do…while
grammar
do{
//执行体
}while(布尔表达式);
for loop
grammar
for(初始化表达式;布尔表达式;步进表达式){
//循环体
}
Note
1. The initialization expression is executed only once. Multiple variables of the same type can be initialized at the same time separated by commas
2. The core of the for loop is two colons. Always true if the boolean expression is omitted, and the step is 0 if the step expression is omitted
for(;;){ //循环 } 等价于 while(true){//循环}
Loops can be nested
Features : If the number of executions is more, it will consume more time. In actual development, try to avoid the nesting of loops. Preferably no more than two levels of nesting.
for(;;){ //外循环
for(;;){ //内循环
}
}
Note : the change speed of the outer loop should be slow, and the change speed of the inner loop should be faster
- continue: means to end the current loop and execute the next loop.
- break: means to end the current entire loop
- Loops can be controlled using tags, using the tag syntax before the loop control statement: tagname: