Loop statement: Perform an operation repeatedly if certain conditions are met. There are three common types of loop statements available in Java:

If the condition is false, the syntax for exiting the loop is as follows: while (conditional expression) {execute statement}

Example:

public class Denon {     public static void main(String[] args) {

        int num = 1;

        while(num<=5){ 

            num++;

System. The out. Print (num + “, “);

        }

    }

}

The output is 2, 3, 4, 5, and 6

Note: The while expression is preceded by “;” “, the program will execute an empty statement, and enter an infinite loop, and the Java editor will not report any errors, which will waste a lot of time debugging.

2, the do… While loop Statements For a while statement, if the condition is not met, the loop cannot be entered. But sometimes we need to do it at least once, even if the conditions are not met. The do… The while loop is similar to the while loop, except that do… The while loop executes at least once with the syntax: do{execute statement} while (conditional expression); Example:

int num = 1; While (num<1){// do not enter the loop num++; System. The out. Print (” while “+ num +”, “); }

do { num++; System. The out. Print (” the do… While “+ num +”, “); }while (num<1); / / do… While loop once, then judge whether the condition is true, output result is 2,

The for loop is one of the most useful loop statements in Java programming. A for loop can be used to execute a statement repeatedly until a condition is satisfied. Statement format: for(initialization; Boolean expression; Update) {// code statement} example:

int num1 = 3; for (int i = 0; i < num1; I ++) {system.out.println (” I output value “+ I +”, “); } I output value 0, I output value 1, I output value

The foreach statement is a special, simplified version of the for statement. It cannot completely replace the for statement, but any foreach statement can be rewritten as a version of the for statement. Foreach syntax: for(declaration statement: expression){// code sentence} example:

Int [] num2 = new int[]{1,2,3,4,5,6}; For (int I: num2) {system.out. print(” I output “+ I +”, “); } I output 1, I output 2, I output 3, I output 4, I output 5, I output 6