Loop structure: syntax, and execution order syntax:
while (Boolean type) {
Function statements
}
Execution order:
1 The value in the break parenthesis is true, and if true, the function statement is executed
2 line up the function statement, continue to determine the value in parentheses, if true, continue to execute the function statement
3 jumps out of the loop until the value in the parentheses is determined to be false
Precautions:
Write value in while parenthesis true dead loop
Write false error in while parenthesis: unable to access statement
Do-while: Syntax structure:
do{
Circular Function Statement C
Post-loop statement D (statement to control loops)
}while ();//cyclic judgment Statement B
Execution order:
- First execute the function code inside the do struct once
- First judgment while () result is ture
- Perform a Do loop body ....
- Second judgment while () result is ture
- Perform a Do loop body ....
- .......
- Nth time judgment while () result is flase Loop end
The difference between the and while:
1. Differences in grammatical structure
The 2.while statement is terminated after the test condition is executed before the statement condition is not met. Do While is the first to execute the statement re-test condition, the condition does not meet after termination
3.do while executing at least once
For loop: syntax structure:
For (Loop initialization statement A; Loop judgment statement B; post-loop statement d) {
Looping function statements
}
Execution order:
Loop Initialization Statement A: executes once
B-->true---->C---->d
B-->true---->C---->d
.....
B---End of >false cycle
Characteristics:
for (;;) {No functional statement} dead loop.
for (;;) After direct with function statement (1 innings) dead loop
for (;;) followed by two function statements, second inning error, for unreachable statements
Precautions:
When for (int I = 1; I <= 10; i++) I is a variable that belongs to the body of the structure and cannot be accessed externally
Loop Nesting: Concepts:
Loop nested loops also have loops that can be nested in multiple layers, and different loop structures can be nested with each other.
For example:
for (int i = 1;i<=5;i++) { //control print line Count for (int k = 1;k<=5-i;k++) { //control to print a line of content System.out.print (""); } for (int j = 1;j<=i;j++) { //control print a line of content System.out.print ("*"); } System.out.println (); }
Suggestions:
- The actual development does not nest too deep, in general case nested enough.
Loop control statement: Break: Features:
1. Loop structure end: does not equal the end of the method, the statement behind the for Loop struct will also run normally
2.break just terminates when the layer loops:
3. If you want to terminate the outer loop in the inner layer: you can loop an alias to the outer.
Syntax: with the break name; ---> End outer loop
Example:
Class breaktest{public static void Main (strinf[] args) { test:for (int i = 0; i<=5; i++) { System.out.println (i); for (int j = 0; J <= 5; j + +) { if (j = = 3) {break test; } }}}
Continue: Features:
Skip the layer as the second cycle, continue the next cycle
Return: Features:
End method when used in a loop, indicating the end of the current method
Precautions:
The break statement, the Coutinue statement, and the return statement cannot be followed by a write statement. Error: Cannot access
Three loop structures and loop nesting in Java