1, Cycle of process control
(1) for loop of loop structure
1. Format:
for (initialization statement; loop judgment condition; change of initialization statement){
Loop body statement;
}
2. Description:
1) Initialization statement: declare a variable to record the number of cycles or the cycle gas point
2) Loop judgment condition: boolean type expression, which controls whether the loop continues to execute
3) Change of initialization statement: change the variables declared in the initialization statement to the direction that the loop cannot be executed
4) Loop body statement: the logical content to be executed repeatedly
3. Execution process
1) Execute initialization statement
2) Calculation cycle judgment condition
3) If the evaluation is false, the for loop ends
4) If the calculation result is true, the loop body statement is executed
5) Changes in execution initialization statements
6) Go back to step two
Example:
Public class Demo02 {public static void main (string [] args) {for (int i = 0; I < 5; I + +) {system.out.println ("the" + (i+1) + "second execution");}}}
Execution results:
4. Precautions
1) The loop judgment condition must be a boolean result
2) Do not have a semicolon before the left brace, otherwise the for loop cannot control the statement in the brace
3) Loop statement. If there is only one sentence, the braces can be omitted.
4) The increment of cyclic variables is not always 1, and can be either positive or negative
Example:
Calculate the sum of all even numbers 1-100
public class Demo03 { public static void main(String[] args) { int sum = 0; for(int i = 1;i<=100;i++){ if(i%2==0){ sum += i; } } System.out.println(sum); } }
(2) while loop of loop structure
1. Format
Initialization statement;
while (loop judgment condition){
Loop body statement;
Change of initialization statement;
}
2. Execution process:
1) Initialization statement
2) Evaluate the conditional expression. If it is false, the loop ends directly
3) If true, execute the loop body statement
4) Changes in execution initialization statements
5) Go back to step two
3. Precautions:
1) The conditional expression must be of type boolean
2) Do not precede the left brace with a semicolon, otherwise the while brace is invalid
3) Don't forget the change of initialization statement, otherwise the loop won't stop
Example:
public class Demo04 { public static void main(String[] args) { int i = 0; while(i<5){ System.out.println("Perform the procedure once"); i++; } } }
Execution results:
Random number class
Get Random integer: Random
1. Guide Package: import java.util.Random;
2. Create Random object
Random r = new Random();
3. Call the function to return a random number of [o,n-1]
Example:
import java.util.Random; public class Demo08 { public static void main(String[] args) { Random r = new Random(); int a = r.nextInt(100); System.out.println(a); } }
(4) Loop jump control statement
Jump statement: it is used to end the loop and control the loop. There are two statements: continue and break
1.continue statement
1.1.continue statement
End this cycle and continue the next cycle
//Use continue to print even numbers from 1 to 100
public static void main(String[] args) { for(int i = 1;i<=100;i++){ if(i%2!=0){ continue; } System.out.println(i); } } }
Execution results:
2.break termination statement
2.1.break statement:
1) Terminate switch statement
2) End current hierarchy loop
Example:
public class Demo11 { public static void main(String[] args) { for(int i = 0;i<5;i++){ System.out.println("Execute once"); if(i==2){ System.out.println("leave"); break; } } } }
Execution results:
(5) Dead cycle
1. Dead cycle: infinite cycle, and the cycle will never stop
2. Format:
1) Dead loop of for statement:
for(;;){
Loop body statement;
}
2) while statement loop: common
while(true){
Loop body statement;
}
3. Function of dead cycle
1) Dead loop is used in the format, but in the loop body, it may be necessary to end the loop. Some jump statements to jump out and terminate the loop are prepared.
2) In the design of the server, we want the server to serve us forever, so we prepare an endless loop
4. Precautions:
The code after the loop will report an error because the code will never execute to
Example:
public class Demo12 { public static void main(String[] args) { /*for(;;){ System.out.println("Non stop "); }*/ while(true){ System.out.println("Non-stop"); } } }
(6) Nested loop
1. Nested loop: the loop body of one loop is another loop.
Total number of nested cycles = number of outer cycles * number of inner cycles;
2. Nested loop format:
for (initialization statement 1; loop condition 2; initialization statement change 7){
for (initialization statement 3; loop condition 4; initialization statement change 6){
Inner circulation body 5;
}
}
Example:
public class Demo15 { public static void main(String[] args) { for(int i=1;i<=4;i++){ for(int j=1;j<=5;j++){ System.out.print("*"); } System.out.println(); } } }
Execution results: