Process control-select structure and loop structure

Flow control structure

There are three flow control structures in Java: sequence structure, selection structure, and loop structure.

Sequence structure

Sequential structure refers to the structure in which the program executes each statement sequentially from top to bottom, without any judgment or jump in the middle.

Choose an institution

There are if statements and switch statements in Java to implement the selection structure.

1. The if statement
if selection structure is processed according to the conditional judgment. There are 3 different forms of if statement, namely single branch structure, double branch structure and multi-branch structure.
(1) Single branch structure of if statement

Syntax format:
if (condition) { //statement } The condition is an expression of Boolean type, and the result of the operation is true and false.


Flow chart of if statement
Insert picture description here

**Flow chart of if statement**

The implementation steps of the if statement are:
1) Judge the conditions in parentheses.
2) If the result of the condition is true, execute the statement in the if structure.
3) If the result of the condition is false, skip the statement in the if structure.

(2) If-else statement multi-branch processing

The grammatical format of if-else statement:
if(condition){ statement 1; //the result in the condition is true, then execute statement 1 }else{ statement 2; //the result in the condition is false, then execute statement 2 }




Insert picture description here

**Flow chart of if-else statement**

(3) Multi-branch if selection structure
When there are multiple values ​​in the condition for selection judgment, it is necessary to use the if multi-branch structure to process.

if(t condition 1) { statement 1;//judgment condition 1, the result is true, execute statement 1; }else if(condition 2) { statement 2;//when condition 1 is false, judge condition 2, and the result is true, Execute statement 2; }else{ statement 3; //When both condition 1 and condition 2 are false, execute statement 3; } There can be multiple else if statements.






2. Nested if statement
If the if selection statement contains one or more if control statements, it becomes a nested if statement. Nested if statements can enhance the flexibility of the program through the collaboration of outer statements and inner statements.
Syntax format of nested if statement:

if(condition1){ if(condition2){ statement 1; //condition 1 is true, judge condition 2, and condition 2 is also true, then execute statement 1 }else{ statement 2;//condition 1 is true, judge If condition 2, condition 2 is false, then execute statement 2 } }else{ if (condition 3) { statement 3;//if condition 1 is false, judge condition 3, condition 3 is true, execute statement 3 }else{ statement 4 ;//If condition 1 is false and condition 3 is also false, execute statement 4 } }











3. Switch statement
Java also provides a switch statement to implement a multi-branch selection structure. It and multi-branch if statement can replace each other in some cases.

The grammatical format of the switch statement:
switch (condition) { case constant 1: //case matches the result of the condition statement; break; // break is used to terminate the execution of subsequent statements and jump out of the entire switch statement. Case constant 2: statement; break; case constant 3: statement; break; default; //default is optional, when other conditions do not match, the default statement is executed ; break; }













Note:
If there is no break statement after the case, the program will continue to execute downwards, even if the corresponding value is matched, the switch statement will be terminated only when the break is encountered. In addition, the switch can only perform equivalent judgments, not comparison judgments, and logic. Judgment etc.

Loop structure

The loop control structure statement in Java language includes while loop statement, do-while loop statement and if loop statement. The characteristic of the loop structure is that when a given condition is met, the statements in the loop are executed repeatedly until the condition is not met.

The entire loop structure can be roughly divided into three parts: the
initial part: set the initial state of the
loop ; loop body: the code statement to be executed repeatedly;
loop condition: determine whether to continue the loop condition.

1.
The syntax format of while loop while loop statement:

Variable initialization
while (loop condition) { loop statement; }

The while statement first judges the loop condition, and executes when the loop condition is satisfied. If the loop condition is not satisfied for the first time, the loop statement in the loop will not be executed once.

Flow chart of the while statement execution steps:
Insert picture description here

2.
Syntax format of do-while loop do-while loop statement:

Variable initialization
do{ loop statement: }while(loop condition);

The do-while loop starts with the keyword do, and finally ends with while and the loop condition in the parentheses, but you must not forget to add a semicolon (";") after the parentheses. The do-while loop statement is to execute the loop statement first In determining the loop condition, the loop statement is executed at least once. When the loop condition is established, continue to execute the loop statement and know that the loop condition is not established.
Flow chart of do-while loop statement:
Insert picture description here

3.
The syntax format of for loop for loop statement:

for (variable initialization; loop condition; modify the value of the loop variable) { loop statement; } The execution steps of the for statement: (1) First perform the variable initialization operation; (2) Then judge the loop condition; (3) If the condition is True, execute the loop statement; (4) The loop statement executes the operation of modifying the value of the loop variable, changes the value of the loop variable, and judges the loop condition again. If the result is true, continue to execute. (5) If the result is false, terminate the loop, jump out of the loop body, and perform the operations following the loop.







No matter how many times the for loop is looped, variable initialization is performed only once. When the number of loops is determined, the for loop is usually used. On the contrary, when the number of loops is uncertain, the while loop and do-while loop are used.

4. Multiple loops
Multiple loops means that the loop body of a loop statement can contain loop statements, also known as nested loops. Multiple loops can be nested within a loop statement, and different loop statements can be nested within each other.

Jump statement

The main function of the jump statement is to control the transfer to other parts of the loop or jump out of the loop directly.

1. Break statement The
break statement can directly terminate the loop in the loop and jump out of the loop body directly. The break statement will only appear in switch statements and loop statements.

2. The
continue statement The function of the continue statement is to force the loop to return early, allowing the loop operation to directly operate the statement following this loop, and then start executing the next loop. In the while and do-while loops, after the continue statement is executed, the program will directly determine the loop condition. If the condition is true, it will continue the loop, otherwise it will terminate the entire loop. In the for loop statement, the continue statement makes the loop first Jump to the loop variable calculation part, and then judge the loop conditions.

3. The return statement The
return statement ends the execution of the current method, and exits and returns to the statement that called the method.

Guess you like

Origin blog.csdn.net/weixin_54524431/article/details/113033967