One, for loop
Format: for (loop variable initial value setting; loop condition Boolean value; statement executed after each loop) {
Circulation body;
}
Execution process: Initial value setting is calculated first.
The Boolean value is then computed, and if true, the loop body is executed once, and then the statement executed after each loop is executed.
The Boolean value is then computed, and if true, the loop body is executed once, and then the statement executed after each loop is executed.
The Boolean value is then computed, and if true, the loop body is executed once, and then the statement executed after each loop is executed.
......
The Boolean value is then computed, and if false, it stops.
Second, while Loops
Statement format: while (Boolean expression) {
statement block;
}
Execution order: Determine the value of the Boolean expression first, if true. The statement block is executed.
The value of the Boolean expression is then judged, if true. The statement block is executed.
The value of the Boolean expression is then judged, if true. The statement block is executed.
......
It ends when the Boolean expression is false.
Third, Do-while Loops
Format: do{
statement block;
}while (boolean-expression);
Execution order: Executes the statement block and then determines the Boolean expression, if True.
Continue execution of the statement block, and then determine the Boolean expression, if True.
Continue execution of the statement block, and then determine the Boolean expression, if True.
Continue execution of the statement block, and then determine the Boolean expression, if False. Stop it.
Four, Break with the Continue
The use of break and continue in the loop.
Break: Stop Looping
Continue: Stop this cycle
V. Summary
Comparison of three types of cycles
1. For the same problem, three kinds of cycles can be substituted for each other.
2, for loop function is stronger than while,do...while, but if the initial value of the loop variable is not obvious, then you can use while or do...while to enhance the structure and readability of the program.
3, * * * Write loop must prevent infinite cycle (dead cycle).
Introduction to Java Basics (iv) Loop structure