IT Band of Brothers Java Grammar loop flow control statements sentence structure 4

do-while loop

There is also a Java loop is the do-while. And for, while at the top of the loop determination of these different conditional expression statement, do-while conditional expression is checked at the bottom of the cycle. This means that at least a do-while loop to execute a loop. do-while loop of the syntax is as follows:

do{

    Loop body;

} While (loop condition);

And while loop is different, there must be a semicolon after do-while loop condition loop, the semicolon indicates the end of the cycle.

Example: The following program illustrates the flow of execution of the do-while loop:

public class DoWhileDemo{

    public static void main(String[] args){

         int count = 1;

         do{

              System.out.println(count);

              count++;

         }while(count < 10);

    }

}

Compile and run this program, the console 17 displays information as shown in FIG.

In the do-while loop, even if the expression loop condition is false start, do-while loop will execute the loop body. Therefore, do-while loop the loop body must be executed once. The following code will verify the correctness of these words:

public class DoWhileDemo{

    public static void main(String[] args){

         int count = 11;

         do{

              System.out.println(count);

              count++;

         }while (count < 10);

    }

}

d4ec80bd8f0f4592807b1d459510d0bd.png

FIG 17 DoWhileDemo operating results

 

Compile and run this program, the console 18 displays information as shown in FIG.

6c5768028d3f41b1a83d6aad7cbc1fcd.png

DoWhileDemo operation result of FIG. 18 modified

From the above procedure, although the start count value is 11, count <10 expression returns false, but the do-while loop or the body of the loop will be performed once.

Guess you like

Origin www.cnblogs.com/itxdl/p/11262001.html