The do...while loop

The syntax of the do...while loop is:

do{}while();

Let's consider the following example, where we want to print the numbers from 20 to 30:

    int j=20;do{    j++;}while(j<30); // 1 loop of execution is guaranteed 

The preceding code will print 20, 21, 22 until 29 as the output. Thus, first it executes and then it compares. 

The basic difference between the while and do...while loop is that the while loop will not execute without evaluating the Boolean expression, and the do...while loop first executes for one loop and then evaluates to run for more loops. 

Let's consider the following example, where the value of the variable is greater than 30:

int j=20;do{    j++;}while(j>30); // 1 loop of execution is guaranteed 

Here, the output ...

Get Hands-On Automation Testing with Java for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.