do while and while loops in Java

The while and do while loops in Java

Among the different types of loops available in Java, the while loop is one of those. In Java while and do while loops, the given block of code keeps on executing as long as the condition given in while statement is true.

The expression used in the while statement must return a Boolean expression. After evaluation in each iteration, if the returned value is true, the statement will keep on executing. As the condition becomes false, the while loop ends and control moves to the next line of code outside the while loop.

The difference

The expression is checked at the top of the while loop and this is the only difference between while and do while loops. In the case of do while loop, the expression is evaluated at the bottom. This results in executing the block of code inside the do while loop at least once even if the condition is false at the start.

In the next sections, I will show you examples of using the do while and while loops, first have a look at syntax:

Syntax of using Java do while loop

The general way of using the do while loop is:

do {

//     Statements to execute inside do while

} while (Boolean_expression);

An example of using the do while loop in java

In this example of demonstrating the do while loop, a variable x is assigned an initial value of 10. After that, a do while loop is used where the value of x is checked in each iteration. If the value is less than or equal to 50, the loop will keep on executing. In each iteration, the value of x is incremented by 10. Have a look:

The do while loop code:

public class loop_demos {

    public static void main(String []args) {

 

        int x;

        x=10;

        do {

               System.out.println("The value of x = " + x);

               x=x+10;

 

        }    

        while (x<=50);

 

        }

}

Output:

Java do while

Syntax of using the while loop

The while loop can be expressed as follows:

while (Boolean_expression) {

 //     Statements to execute inside while loop

}

A demo of using a while loop

In this example, the x variable is assigned an initial value of 50. In each iteration, the value of x is decremented by 10. The condition in the while loop is to keep on executing the block of code until the value of x is greater than 10.

Java code:

public class loop_demos {

    public static void main(String []args) {

 

        int x;

        x=50;

        while (x >= 10) {

               System.out.println("x = " + x);

               x=x-10;

 

        }    

        }

}

Output:

Java while

The demo to show the difference between Java do while and while loops

In this example to show the difference between while and do while loops, I declared a variable x with an initial value of 51. In both loops, the same condition is used:

x <= 50

As this is false, see the difference in output as using both loops in single program:

The code of while and do while loops:

public class loop_demos {

    public static void main(String []args) {

 

        int x;

        x=51;

        // Running while loop

        while (x <= 50) {

               System.out.println("x = " + x);

               x=x-10;

 

        }  

        // Running do while loop       

        do {

            System.out.println("The value of x (in do while) = " + x);

            x=x+10;

 

       }    

        while (x <= 50);

 

        }

}

Output:

while do while loops

In the output of this program, you can see the do while statement is executed once whereas, in the while loop, the statement is not executed. In both cases, the condition was false, however, as do while evaluates the condition in the bottom, it executed the statement in do while block.

What loop to use?

In Java programs, generally, you would prefer using the for loop for iteration of the block of code, working with arrays or other collections etc. The for loop is versatile and has an enhanced form that enables working with arrays and collections in Java.

It’s up to you deciding what type of loop to use, however, the do while is particularly useful where you want a block of code to execute at least once to see the outcome before the condition is evaluated.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!