Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Do you use the three loop structures in Java?


May 30, 2021 Article blog


Table of contents


1.for loop

format:

for (initialization statement 1; J udgment Statement 2; S tep Statement 3)
Loop code 4
}

To execute the process:

First execute 1 initialization statement
Execute 2 judgment statements if the judgment results in true
Execute the 4-loop code
Execute a 3-step statement
Continue back to 2 4 3 2 4 3
If the result is false, it represents the end of the loop
note:
The initialization statement is executed only in the first loop and no longer after that

Code walkthrough:

public static void main(String[] args) {

/*

Judgment 0-10 qi

*/

// Even: The number that can be referred to by 2 is called an even number

// 0 is a special even number

for(int i=0;i<=10;i++){

if (i%2 == 0){

System.out.println (i + "is even");

}else {

System.out.println (i + "odd");

}

}

}

2.while loop

format:

Initialization statement 1;
while (judgment statement 2)
Loop code 3;
Step statement 4;
}

To execute the process:

1 Initialize the statement
2 Execute the judgment statement, execute the code in braces if it is true, and end the loop if it is false
3 Loop code
4 step statement
Loop through the 2 judgment statements again

Code walkthrough:

public static void main(String[] args) {

/*

Seeking 1-100 accumulation and

*/

int x = 1;

int sum = 0;

while (x<=100){

sum += x;

x++;

}

System.out.println ("1-100 is accumulated and:" + Sum);

}

3.do-while loop

format:

Initialization statement 1;
do{
Loop Statement 2;
Step Statement 3;
while (judgment statement 4);

To execute the process:

1 Initialize the statement
2 Loop statement
3 step statement
4 Judgment statement, if the result of the judgment is true to continue the loop, if the result of the judgment is false to end the loop
note:
The do-while loop is executed after execution, regardless of whether the condition is met or not

Code walkthrough:

public static void main(String[] args) {

/*

Output 1-10 and

*/

int a = 1;

int sum = 0;

do {

sum += a;

a++;

}while (a<=10);

System.out.println ("1-10 and:" + Sum);

}

Three looping usage scenarios

All three cycle structures can achieve corresponding functions and choose a simpler form according to actual needs. Here's a summary of the usage scenario:

  • When identifying the number of cycles: For loops are recommended
  • When the number of cycles is not clear: It is recommended to use a while loop
  • do... W hile loop is more special, it first executes the loop body, and then according to the judgment criteria to decide whether to execute the loop again, that is, do... while performing a loop at least once

Loop nesting

Code Walkthrough 1

public static void main(String[] args) {

/*

Cycle nested:

Outer cycle is performed once, the memory loop is executed

*/

/*

Circular from 0:0am - 23:59

*/

// Traverse the hour

for (int i=0; i<24; i++){

// Traverse minute

for (int j=0; j<60; j++){

System.out.println (i + "point" + J + "points");

}

}

Code Walkthrough 2

public static void main(String[] args) {

for (int i = 1; i < 10; i++) {

System.out.println ("You are the" + i + "Bit Customer");

while (true) {

System.out.println ("Please select the item you want to purchase: 1. Fruit 2. Vegetables 3. Beverage 4. End purchase");

Scanner sc = new Scanner(System.in);

int a = sc.nextInt();

if (a == 1) {

System.out.Println ("You buy fruit");

} else if (a == 2) {

System.out.Println ("You buy vegetables");

} else if (a == 3) {

System.out.println ("You purchased a drink");

} else if (a == 4) {

break;

} else {

System.out.println ("The instruction you entered");

}

}

}

}

The writing of two dead loops

1.while(true){...}
2.for(; ;){...}

Recommended lessons: Java micro-class, Java multithreaded explanation