Circular structure statement
Loop statements are used to make code execute conditionally or unconditionally
There are three types of cyclic structures:
1.while loop structure
2.do...whlie cycle structure
2.for loop structure (foreach loop structure, enhanced version of for loop structure)
1. While loop structure
The while loop structure is the same as the if structure type (judgment condition). While also needs to receive a judgment condition, which needs to be a boolean value. The judgment condition is relatively extensible.
If the judgment condition is true, the loop executes the content in the judgment code block, and the content wrapped with {} is called the loop body.
Syntax format of while loop structure:
while(Judgment conditions) { Circulatory body }
Summary:
First, check whether the judgment condition in the while loop structure is true. If the condition is true, execute the code block in the loop body; After the code block is executed, it will continue to judge the current whlie loop conditions. If the conditions are full, it will continue to repeat the above operations. As long as the result value of the judgment condition is false, the current while loop result is terminated and the content in the code block corresponding to the loop body is skipped.
To ensure that the conditions of the while loop have termination conditions, generally, the conditions of the loop are controlled by the volume in the loop. If the judgment condition is true, it will fall into an endless loop.
2 do...while loop structure
Is a loop structure, structurally and while loops are also extremely type.
Syntax structure of do...while loop:
do { Circulatory body } while(Judgment conditions);
Grammatical features:
1. The whole do...while loop structure needs to use ";" as the end of the statement;
2. When executing the do...while structure, first directly enter the {} interior, that is, directly execute the loop structure;
3. Judge whether the condition is true. If it is sorted, enter the structure of the loop again and execute the code; On the contrary, only the exit do...while loop result is written, followed by the code block after the structure.
3. Difference between while loop and do...while loop
1. The while loop first judges the conditions. If the conditions are not true, the contents in the loop code block will not be executed
2.do...while loop first executes the contents of the loop code block, and then judges whether the loop condition is true.
3.do...while loop structure whether the condition is true or not, the contents in the loop body will be executed once.
4 for loop structure
While loop and do... This structure is only used when judging the conditions of while.
The for loop result is used to operate a loop operation with a fixed number of cycles (the number of cycles is known).
for loop structure syntax format:
example: for (i=1;i<=5;i++) for(Loop initialization①;Loop execution conditions②;Cycle step④){ Circulatory body③ }//The execution sequence is 1234 234 234
Syntax parsing process of for loop:
Step 1: execute 1 first (initialization operation)
Step 2: execute 2 operations (if the judgment condition is not true, execute the end program; if the condition is true, execute later)
Step 3: execute 4 operations (the loop body is executed once)
Step 4: execute 3 operations (control the termination conditions of the whole loop, operate the expression, and operate the initialized variables)
Step 5: execute 2 operations (judge conditions) and repeat step 3
Step 6: exit the for loop result
Case: use the for loop structure to find the sum of all numbers between 1-100. Analysis: the number of cycles is fixed. It is recommended to use the for loop structure.
public class Example03 { public static void main(String[] args) { int count = 0; for(int i = 1; i <= 100; i++) { count += i; } System.out.println("1-100 And:" + count); } }
Summary:
1. When the number of cycles is fixed, use the for loop result to operate.
2. Sequence of for loop operations: initialization expression - judgment condition - loop body - operation expression - judgment condition
5 loop nesting
The new loop result is nested in the loop structure. The number of nested tests is not required (most scenes are nested twice). You can nest any loop in any loop. Most loop nesting is for loop nesting.
For loop nested for loop syntax structure:
for(Initialization expression;Judgment conditions;Operation expression) { .... for(Initialization expression;Judgment conditions;Operation expression) { Circulatory body } .... }
Use for loop nesting to complete the printout of right triangle (the number of cycles is fixed).
public class Example04 { public static void main(String[] args) { /** * The scope (use range) of a variable depends on the first variable before it * {Parentheses, */ // Controls the number of lines printed for (int i = 1; i<=9; i++) { // The number of * corresponding to the current row matches the number of rows // The value of i of the outer loop at this time determines the number of times of each inner loop for (int j = 1; j<=i;j++) { /** * print():Print out the contents of parameters without line wrapping * println():Print and input the contents of parameters, and then perform line feed operation */ System.out.print("*"); } System.out.println(); } } }
6 jump statement
Often in the loop structure, jump operation is required, and jump statements are used to solve this problem. Two keywords are provided:
-
break:
(1) If used in the switch result, a case statement is terminated and the result of the current switch statement is exited.
(2) If it is used in the cycle result, the current cycle result is directly terminated, that is, the cycle result is exited.
(3) If break occurs in the content of a nested loop, it will jump out of the inner loop instead of the outer loop.
-
continue:
It can be used in combination with loop statements to skip this loop and execute the next loop structure.
public class Example05 { public static void main(String[] args) { int x = 1; while(x<4) { System.out.println("x=" + x); if (x == 3) { break; // If the value of x is 3, it will directly jump out of the while loop structure } x++; // Auto increment operation } } }
Use break cases in double-layer loops:
public class Example06 { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int j=1; j<=i; j++) { if (i>=4) { break; } System.out.print("*"); } // Line feed System.out.println(); } } }
Find the sum corresponding to the odd number of 1-100.
public class Example07 { public static void main(String[] args) { // Find the sum of odd numbers int num = 0; for (int i = 1; i <= 100; i++) { if (i%2==0) { // Indicates that this is an even number // Skip this cycle and enter the next cycle // From the current corresponding to continue, skip all subsequent codes, and then enter the next cycle continue; } num += i; } } }