Detailed Java loop statement (Java essential knowledge)

A loop statement is to perform an operation repeatedly when certain conditions are met. There are four commonly used loop statements in Java, namely while statement, do...while statement, for statement and foreach statement. The foreach statement is a special simplified version of the for statement, which will be introduced below.

while loop statement

The loop method of the while statement is to control whether to continue to execute the statement repeatedly through a condition.

The syntax is as follows :

while(条件表达式){
    
    
	执行语句
}

When the return value of the conditional expression is true, the statement in {} is executed. After the statement in {} is executed, the return value of the conditional expression is judged again, until the result of the expression is false, exit the loop .

Code example :

do...while loop statement

The do...while loop statement is similar to the while loop statement. The difference between them is that the while statement first determines whether the condition is true, and then executes {}. The do...while loop statement executes the loop first, and then determines whether the condition is true. That is, the {} code in do...while is executed at least once.

The syntax is as follows :

do{
    
    
	执行语句
}
while(条件表达式);

One obvious difference between the do...while statement and the while statement is that there is an extra semicolon at the end of do...while.

Code example :

for loop statement

The for loop can be used to repeatedly execute a statement until a certain condition is met.

The syntax is as follows :

for(表达式1;表达式2;表达式3){
    
    
	语句
}

Expression 1: Usually an assignment expression, responsible for setting the initial value of the loop, that is, assigning values ​​to the variables that control the loop.
Expression 2: It is usually a relational expression, which compares the loop control variable with the allowable range value of the loop variable.
Expression 3: It is usually an assignment expression to increase or decrease the variable that controls the loop.

Code example :

foreach statement

The foreach statement is a special simplified version of the for statement, but foreach cannot completely replace the for statement. Not any foreach statement can be changed to the for statement version. The
foreach statement is very convenient in traversing arrays.

The syntax is as follows :

for(循环变量x: 遍历对象obj){
    
    
	应用了x的java语句
}

Traverse the object obj: read the values ​​of the elements in obj in turn.
Loop variable x: Assign the value read by obj traversal to x.

Examples are as follows :

Loop nesting example

Jump statement

break

Use the break statement to jump out of the switch structure. In the loop structure, break can also be used to jump out of the current loop body.

The above is only a single-layer loop. For multi-layer loops, when you only want to jump out of one layer, break also jumps out of the specified loop.

The syntax is as follows :

标签名: 循环体{
    
    
	break 标签名;
}

Examples are as follows :

continue statement

The continue statement is a supplement to the break statement. Continue does not jump out of the loop body, but skips the statement before the end of the loop, returns to the loop condition test part, and restarts the loop.

Like break, continue also supports the label function, which is mainly used for multi-layer loops.

The syntax is as follows :

标签名 : 循环体{
    
    
	continue 标签名;
}

Guess you like

Origin blog.csdn.net/weixin_43888891/article/details/112797881