Java basic flow control statement

Classification of control statements in Java:

(1) Branch statement: if-else, switch.
(2) Loop statement: while, do-while, for.
(3) Jump statements: break, continue, return.
(4) Exception handling statement: try-catch-finally, throw.
(5) Package processing statement: package, import.
(6) Comment statement: //, / *… /, / ** /

Standard if-else statement

The third format of if statement: if…else if…else
if (judgment condition 1) { execution statement 1; }else if (judgment condition 2) { execution statement 2; } }else if (judgment statement n) { execution statement n; }else{ execute statement n+1; }









Implementation process

First judge the relational expression 1 to see whether the result is true or false.
If it is true, execute the statement body 1 and
if it is false, execute the statement body 2.

Insert picture description here
Insert picture description here

if… else if …else

Insert picture description here
Insert picture description here

switch statement

switch (expression) { case constant value 1; statement body 1; break; case constant value 2; statement body 2; break; default: statement body n+1; break; } first calculate the value of the expression , then, and case Comparing in turn, once there is a corresponding value, the corresponding statement will be executed. During the 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. 1. The values ​​behind multiple cases cannot be repeated . The parentheses after switch can only contain the following data types; basic data types: byte/short reference data types: String string, enum enumeration














Insert picture description here
Insert picture description here



Insert picture description here
Insert picture description here

for loop statement format

for (initialization expression 1; Boolean expression 2; stepping expression 4) {
loop body 3
}
execution flow
Execution sequence: 1234>234>234...2 until it is not satisfied.
1 Responsible for completing the initialization of loop variables
2 Responsible for judging whether the loop condition is full, and jumping out of the loop if it is not satisfied
3 County executes the statement
4 After the loop, the change of variables involved in the loop condition

Insert picture description here
Insert picture description here

Standard format of do-while/for-while loop statement

do{
loop body
} whiIe (conditional judgment);
extended format:
initialization statement
do{
loop body
step statement

}While (conditional judgment);
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Loop control statement continue keyword

Once executed. Immediately skip the remaining content of the current cycle, and immediately start the next cycle
Insert picture description here
Insert picture description here

Ternary operator and standard if-else statement

Insert picture description here
Insert picture description here

for-if statement

Insert picture description here
Insert picture description here

Exercise after class: use loops to calculate even numbers between 1-100

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_55680204/article/details/114952451