This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

Sequence structured program statements can only be executed once. If you want the same operation to be performed multiple times, you need to use a loop structure.

There are three main types of loops in Java:

  • The while loop
  • The do… The while loop
  • The for loop

The while loop

While is the most basic loop. It has the structure:

While (Boolean expression) {// loop contents}Copy the code

The loop continues as long as the Boolean expression is true.

The do… The while loop

In the case of a while statement, if the condition is not met, it cannot enter the loop. But sometimes we need to execute at least once, if not once.

The do… A while loop is similar to a while loop, except that the do… The while loop executes at least once.

Do {// code statement}while(Boolean expression);Copy the code

Note: The Boolean expression comes after the body of the loop, so the block executes before detecting the Boolean expression. If the value of the Boolean expression is true, the block is executed until the value of the Boolean expression is false.

The for loop

While all loops can be constructed using while or do… While says, but Java makes some loop constructs simpler by providing another statement, a for loop.

The number of times the for loop executes is determined before execution. The syntax is as follows:

For (initialization; Boolean expression; Update) {// code statement}Copy the code

A few notes about the for loop:

  • Perform the initialization step first. You can declare a type, but you can initialize one or more loop control variables, or it can be an empty statement.
  • Then, the value of the Boolean expression is checked. If true, the body of the loop is executed. If false, the loop terminates and execution begins after the body of the loop.
  • After a loop is executed, the loop control variable is updated.
  • Check the Boolean expression again. Loop through the above procedure.

Java enhances the for loop for in

Java5 introduces an enhanced for loop that is primarily used for arrays.

Java enhanced for loop syntax format:

For (statement: expression) {// code sentence}Copy the code

Declaration statement: Declare a new local variable whose type must match the type of the array element. Its scope is limited to the loop block, and its value is equal to the value of the array element at that point.

Expression: An expression is the name of the array to be accessed, or a method that returns an array of values.

/** * Java loop structure */
public class learn_7 {
    public static void main(String[] args) {
        //whlie
        int a = 10;
        while (a < 20) {
            System.out.println(a);
            a++;
        }
        System.out.println("\n");
        //do while
        int b = 10;
        do {
            System.out.println(b);
            b++;
        }while (b < 10);
        System.out.println("\n");
        //for
        for (int x = 10; x <= 20; x++) {
            System.out.println(x);
        }
        System.out.println("\n");
        int [] numbers = {10.20.30.40.50};
        for(intx : numbers ){ System.out.println( x ); }}}Copy the code