The while and do while loops in Java
Among the different types of loops available in Java, the while loop is one of those.
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 the 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:
The value of x = 20
The value of x = 30
The value of x = 40
The value of x = 50
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:
x = 40
x = 30
x = 20
x = 10
The demo to show the difference between Java do while and while loops
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 a 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:
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 the do while evaluates the condition in the bottom, it executed the statement in the do while block.
Which 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 to decide 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.