3 demos of 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:

Java do while

See online demo and code

The do while loop code:

 

Syntax of using the while loop

The while loop can be expressed as follows:

 

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 while

See online demo and code

The while loop code:

 

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 the both loops in single program:

while do while loops

See online demo and code

The code of while and 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 collection 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.