For some same or similar statements that need to be executed repeatedly, use a certain format to simplify the code.
Classification:
- while Loop
- do... while loop
- for loop
- jdk5 introduces an enhanced for loop that is mainly used for arrays
while Loop
Format:
while(Boolean expression){ //Cyclic content }
matters needing attention:
- As long as the Boolean expression is true, it will continue to execute
- In most cases, we will stop the loop. We need a method to invalidate the expression to end the loop.
- In a few cases, the loop needs to be executed all the time, such as the server's request response listening, etc.
- If the loop condition is always true, it will cause infinite loop [dead loop]. We should try our best to avoid dead loop in normal business programming, which will affect the program performance or cause the program to get stuck and crash.
Execution process:
- Initialization statement
- Evaluate the conditional expression. If it is false, the loop ends directly
- If true, execute the loop body statement
- Execute auto increment of initialization variable
- Go back to step two
public class WhileDemo { public static void main(String[] args) { //Output 1 ~ 100 int i = 0; while(i<100){ i++; System.out.println(i); } } }
public class WhileDemo { public static void main(String[] args) { //Calculate 1 + 2 + 3 ++ 100=? int i = 0; int sum = 0; while (i < 100) { sum = sum + i; i++; } System.out.println(sum); } }
do...while loop
For the while statement, if the condition is not met, it cannot enter the loop. But sometimes we need to implement it at least once if we don't meet the conditions in time.
The do... While loop is similar to the while loop, except that the do... While loop is executed at least once.
Format:
do{ //Code statement }while(Boolean expression);
The difference between while and do... While
- while is judged before execution. do... while is to execute first and then judge
- do... while always ensures that the loop will be executed at least once, which is their main difference
public class DoWhileDemo { public static void main(String[] args) { //Calculate 1 + 2 + 3 ++ 100=? int i = 0; int sum = 0; do{ i++; sum += i; }while(i<100); System.out.println(sum); } }
public class DoWhileDemo { public static void main(String[] args) { int a = 0; while(a < 0){ System.out.println(a); } System.out.println("============="); do { System.out.println(a); }while (a<0); } }
for loop
Format:
for(initialization; Boolean expression; to update){ //Code statement }
Execution process:
- Execute initialization statement
- Boolean expression to evaluate a loop
- If the calculation result is false, the for loop is ended
- If the calculation result is true, the loop body statement is executed
- Execute update
- Go back to step 2
explain:
- Initialization statement, declaring a variable to record the number of cycles
- The Boolean expression of the loop, which controls the conditions of the loop
- Update changes the variables declared in the initialization statement, generally in the direction that the loop cannot execute
- Code statement is the content to be executed repeatedly
- for loop is a general structure that supports iteration. It is the most effective and flexible loop structure
- The number of times the for loop is executed is determined before execution
public class ForDemo { public static void main(String[] args) { int a = 1;//Initialization condition while (a <= 100) {//Conditional judgment System.out.println(a);//Circulatory body a += 2;//iteration } System.out.println("while End of cycle"); //initialization; Condition judgment; iteration for (int i = 1; i <= 100; i++) { System.out.println(i); } System.out.println("for End of cycle"); } }
public class ForDemo { public static void main(String[] args) { //Calculates the sum of odd and even numbers between 0 and 100 int oddSum = 0;//Save odd sum int evenSum = 0;//Save even sum for (int i = 0; i <= 100; i++) { if (i % 2 != 0) {//Odd number oddSum += i; } else {//even numbers evenSum += i; } } System.out.println("The sum of odd numbers is:" + oddSum); System.out.println("The sum of even numbers is:" + evenSum); } }
public class ForDemo { public static void main(String[] args) { //Use the while or for loop to output the number that can be divided by 5 between 1 and 1000, and output 3 per line for (int i = 0; i <= 1000; i++) { if(i%5==0){ System.out.print(i+"\t"); } if (i%15==0){ System.out.print("\r\n"); } } } }
public class ForDemo { public static void main(String[] args) { //Print 99 multiplication table for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + "*" + i + "=" + i * j + "\t"); } System.out.println("\n"); } } }
Enhanced for loop
Format:
for(Declaration statements: expressions){ //Code sentence }
Declaration statement: declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the circular statement block, and its value is equal to the value of the count array element.
Expression: an expression is the name of the array to be accessed, or a method whose return value is an array.
public class ForDemo { public static void main(String[] args) { int[] numbers = {10,20,30,40,50};//An array is defined //Traverse array elements for(int x:numbers){ System.out.println(x); } } }