Note: It is similar to the flow control and loop statement in java. If you already know the flow control and loop statement in java, please skip this blog by yourself
Flow control statement
The programs are executed in order from top to bottom ,
The sequence of program execution can be changed through flow control statements, or a certain section of the program can be executed repeatedly.
classification:
1. Conditional judgment statement
2. Conditional branch statement
3. Loop statement
1. Conditional judgment statement
Conditional judgment statement is also called if statement
Syntax 1:
if(conditional expression){
Statement...
}
Implementation process:
When the if statement is executed, the conditional expression will be evaluated first,
If the value is true, execute the statement after the if
If the value is false, do not execute
Grammar 2:
if(conditional expression){
Statement...
}else{
Statement...
}
Implementation process:
When the if...else statement is executed, the conditional expression will be evaluated.
If the value is true, execute the statement after the if
If the value is false, the statement after the else is executed
Syntax 3:
if(conditional expression){
Statement...
}else if(conditional expression){
Statement...
}else if(conditional expression){
Statement...
}else if(conditional expression){
Statement...
}else{
Statement...
}
Implementation process
When the if...else if...else statement is executed, the conditional expression will be evaluated from top to bottom.
If the judgment result is true, the statement after the current if is executed, and the statement ends after the execution is complete.
If the result of the judgment is false, then continue to judge downwards until a true one is found.
If all the conditional expressions are false, the statement after the else is executed
2. The switch statement
grammar:
switch(conditional expression){
case expression:
Statement...
break;
case expression:
Statement...
break;
case expression:
Statement...
break;
default:
Statement...
break;
}
Implementation process:
When the switch...case... statement is executed, the value of the expression after the case and the value of the expression after the switch will be compared in turn.
If the comparison result is false, then continue to compare downward. If the result of the comparison is true, the code is executed downward from the current case.
If all the case judgment results are false, the code will be executed from the default.
3. Loop statement
Some statements can be executed multiple times through loop statements
while loop
grammar:
while(conditional expression){
Statement...
}
Implementation process:
When the while statement is executed, the conditional expression will be evaluated first,
If the judgment result is false, the loop is terminated
If the judgment result is true, the loop body is executed
After the loop body is executed, continue to evaluate the conditional expression, and so on
do...while loop
grammar:
do{
Statement...
}while(conditional expression)
Implementation process:
When do...while is executed, the loop body after do is executed first, and then the conditional expression is judged.
If the judgment result is false, the loop is terminated.
If the judgment result is true, continue to execute the loop body, and so on
The difference with while:
while: judge first and then execute
do...while: execute first and then judge
do...while can ensure that the body of the loop is executed at least once.
for loop
grammar:
for(①initialization expression; ②conditional expression; ④update expression){
③Statement...
}
Implementation process:
First execute the initialization expression to initialize a variable,
Then evaluate the conditional expression of ②, and terminate the loop if it is false
If the judgment result is true, execute the loop body ③
After the loop body is executed, execute ④ update expression to update the variable.
Repeat after the update expression is executed ②
Recommended
Recommended browser: Google Chrome
Recommended Java environment: IDEA (IntelliJ IDEA)
Recommended learning this week: JavaScript and CSS
Recommended learning video link: https://ke.qq.com/course/231577?taid=2841395744442521
Recommended practice Java environment: https://leetcode-cn.com/