Loop structure,
I. while statement format
Initialize the conditional expression;
While (condition expression) // when the condition is true, the statements in the loop are always executed, and the endless loop is executed. Therefore, you generally need to control the conditions {// execute the statement ;}
Ps: use ctrl + c to stop the endless loop under doc
Ii. do whle statement format
Do {// execution statement;} while (Condition Statement); // when do while has an English semicolon, it indicates that do while ends
Ps:
While first judge the condition, only when the condition is true to execute the loop body
Do while first executes the loop body, then judges the condition, the condition is true, and then continues to execute the loop body
That is, do while will execute a loop body no matter whether the condition is true or not.
3. for Structure
For (initial expression, cyclic condition expression, and cyclic Operation expression) {// cyclic statement ;}
Example of the for Loop execution sequence
For (int x = 0; x <6; x ++)
{
System. out. println ("x =" + x );
}
First, execute the for statement to determine whether the conditions are true and then perform a loop.
1. When int x = 6 is executed, the memory space is opened. The value is defined as x and the value is 0. The initialization expression is only executed once.
2. If x <6 is true, the output statement is executed.
3. After the output statement is executed, execute x ++.
4. Execute the "x <6" command to determine whether the statement is true. The result is "Step 2" and "Step 3". If the statement is false, the loop ends, and the space occupied by x is released.