[Java from entry to soil] flow control statement

Flow control statement

During the execution of a program, the execution order of each statement has a direct impact on the result of the program. In other words, the flow of the program has a direct impact on the results of the operation. So we must be clear about the execution flow of each statement. Moreover, many times we have to control the execution order of statements to achieve the functions we want to complete.
Sequence structure:

public static void main(String[] args){
    
     
    //顺序执行,根据编写的顺序,从上往下运行 
    System.out.println(1); 
    System.out.println(2); 
    System.out.println(3);
}

Judgment statement

if statement

if(关系表达式) {
    
    
    语句体; 
}

Execution process
① First judge the relational expression to see if the result is true or false
② If it is true, execute the statement body
③ If it is false, do not execute the statement body

if...else statement

if(关系表达式) {
    
     
    语句体1; 
} else {
    
     
    语句体2; 
}

Execution process
① First judge the relational expression to see if the result is true or false
② If it is true, execute statement body 1
③ If it is false, execute statement body 2

if…else if…else语句

if (判断条件1) {
    
     
    执行语句1; 
} else if (判断条件2) {
    
     
    执行语句2; 
} else if (判断条件n) {
    
     
    执行语句n; 
} else {
    
     
    执行语句n+1; 
}

Execution process
①First judge condition 1 to see if the result is true or false
②If true, execute statement 1
③If it is false, then judge condition 2
④If true, execute statement 2
⑤And so on...

Select statement

switch statement

switch(表达式) {
    
     
    case 常量值1: 语句体1; break; 
    case 常量值2: 语句体2; break;
    ... 
    default: 语句体n+1; break; 
}

Execution process
① first calculate the value of the expression
② secondly compare with case, once there is a corresponding value, the corresponding statement will be executed, in the process of execution, it will end when it encounters a break.
③Finally, if all the cases do not match the value of the expression, the body of the default statement will be executed, and the program will end.
The penetrability of the case
In the switch statement, if the break is not written after the case, the penetrating phenomenon will occur, that is, the value of the next case will not be judged, and it will run directly until it encounters the break or the overall switch ends. .

loop statement

A loop statement can execute a certain piece of code repeatedly when the loop condition is met. The code that is repeatedly executed is called a loop body statement. When the loop body is executed repeatedly, the loop judgment condition needs to be changed to false to end the loop, otherwise the loop will continue to execute, forming an endless loop.

for loop statement

for(初始化表达式①; 布尔表达式②; 步进表达式④) {
    
     
	循环体③ 
}

Execution process
Execution sequence: ①②③④>②③④>②③④…②Unsatisfied.
① responsible for completing the loop variable initialization
② responsible for determining whether the loop condition is not satisfied out of the loop
statements execute specific ③
④ After cycling, cycling conditions change of variables involved

while loop statement

初始化表达式① 
while(布尔表达式②) {
    
     
    循环体③ 
    步进表达式④ 
}

Execution process
Execution sequence: ①②③④>②③④>②③④…②Unsatisfied.
①Responsible for completing the initialization of loop variables.
②Responsible for judging whether the loop condition is met, and if it is not met, it will jump out of the loop.
③The specific execution statement.
④After the loop, the change of loop variables

do...while loop statement

初始化表达式① 
do {
    
     
    循环体③ 
    步进表达式④ 
} while(布尔表达式②);

Execution process
Execution sequence: ①③④>②③④>②③④…②Unsatisfied.
①Responsible for completing the initialization of loop variables.
②Responsible for judging whether the cycle conditions are met, and if they are not met, they will jump out of the cycle.
③The specific executed statement
④After the loop, the change of the loop variable
do...The characteristics of the while loop
Unconditionally execute the loop body once, even if we write the loop condition directly as false, it will still loop once. Such a loop has a certain risk, so beginners are not recommended to use the do...while loop.

Jump statement

The jump statement is used to realize the jump of the program flow during the loop execution.

break statement

Use scenario: Terminate switch or loop
①In the switch statement of the selected structure
②In the loop statement

public static void main(String[] args) {
    
    
    int x = 1;
    while(x <= 4) {
    
    
        System.out.println("x="+x);
        if(x == 3) {
    
    
            break; //条件成立则跳出循环
        }
        x++;
   }
}

continue statement

Usage scenario: End this cycle and continue to the next cycle

public static void main(String[] args) {
    
    
    int sum = 0;
    for(int i = 1; i <= 100; i++) {
    
    
        if(i % 2 == 0) {
    
    
            continue; //结束本次循环
        }
        sum = sum + i; //求1~100之间的奇数求和
    }
    System.out.println("sum= "+sum);
}

Endless loop

That is, the condition in the loop is always true, and the endless loop is a loop that never ends.

while(true) {
    
    
    System.out.println("死循环了.....");
}

Nested loop

The so-called nested loop means that the body of one loop is the body of another loop. For example, there is a for loop inside the for loop, which is a nested loop. Total number of cycles = number of outer cycles * number of inner cycles

for(初始化表达式①; 循环条件②; 步进表达式⑦) {
    
     
    for(初始化表达式③; 循环条件④; 步进表达式⑥) {
    
     
        执行语句⑤;
    } 
}

Nested loop execution process:
execution sequence: ①②③④⑤⑥>④⑤⑥>⑦②③④⑤⑥>④⑤⑥, outer loop once, inner loop multiple times.
For example, skipping rope: there are 5 groups of jumps, 10 jumps in each group. 5 groups are the outer loop, and 10 are the inner loop.

Guess you like

Origin blog.csdn.net/weixin_51678389/article/details/109155540