[Java] Java based loop structure

        In the Java loop structure, there are three commonly used loops:

for loop, while loop and do...while loop.

for loop

        Code format:

for(初始化语句;条件判断语句;条件控制语句){
    循环体语句;
}

Implementation process:

1) Execute the initialization statement

2) Execute the conditional judgment statement to see if the result is true or false

      If it is false, the loop ends,

      If it is true, the loop continues

3) Execute the loop body statement

4) Execute conditional control statements

5) Go back to 2) Continue execution

while loop

        Code format:

初始化语句;
while(条件判断语句){
    循环体语句;
    条件控制语句;
}

While loop execution process:

①Execute the initialization statement

②Execute conditional judgment statement to see if the result is true or false

       If it is false, the loop ends

       If it is true, continue execution

③Execute loop body statement

④Execute conditional control statement

⑤ back continue 

do...while loop structure

        Code format:

初始化语句;
do{
    循环体语句;
    条件控制语句;
}while(条件判断语句);

Implementation process:

① Execute initialization statement

② Execute loop body statement

③ Execute conditional control statement

④ Execute the conditional judgment statement to see if the result is true or false

        If it is false, the loop ends

        If it is true, continue execution

⑤ Back to Continue 

The difference between the three cycles:

        1) For loops and while loops both judge the condition once and then loop. There may be no loops , but the do...while loop loops first and then judges the conditions. Regardless of whether the conditions are true, the loop will be executed once

        2) The for loop is suitable for the situation where the loop is known at this time, while and do...while are suitable for the situation where the number of cycles is unknown

Three formats of infinite loop (infinite loop)

1. for( ; ; ){}

2. while(true){}

3. do {} while(true); 

 Jump control statement (master) 

Jump control statement (break )

Jump out of the loop, end the loop

Jump control statement (continue)

Skip this cycle and continue to the next cycle

Note: continue can only be used in a loop! Break can also be used in switch!

Guess you like

Origin blog.csdn.net/weixin_43267344/article/details/107566305