Java while and do-while Loops

This lesson will explore the java while loop and java do-while loop. But first, let’s see what loops are in general.

What are loops in Java?

We use loops to execute a certain block of code or just one statement a controlled number of times.
The value of the logical expression controls the number of repetitions of a particular code block in loops.

This means that the statement or the code block would be repeated as long as the value of that logical expression is equal to true, and it will stop the moment the value of that logical expression becomes false.

There are three types of loops in Java:

In the previous tutorial, we covered Java for loopsIn this tutorial, we will cover while and do-while loops.

Java while loop

In the while loop, the execution of its body is repeated as long as the value of a logical expression is equal to true. This repetition is interrupted when the value becomes false.

The syntax of the while loop:

 while(expression) {
   // body of the loop
 }

The while loop is executed by first calculating the value of the logical expression in parentheses.  If a value obtained is equal to false, the while loop execution is immediately interrupted. This means that the rest of the loop is skipped, and program execution continues normally but continues from the code following the while loop.

If the value is equal to true, the loop’s body will execute. Then, the logical expression in parentheses is recalculated, and the same cycle is repeated until the value of the expression becomes false.

The flow of the while loop looks like this:

java while loop


Example of the Java while loop:

class Test {
    
  public static void main(String[] args) {
    int data = 5;
        
    while (data < 10) {
      System.out.println("Data: " + data);
      data++;
    }
        
    System.out.println("Code after the while loop...");
  }
}
Output: Data: 5 Data: 6 Data: 7 Data: 8 Data: 9 Code after the while loop…
 
See how we had to increase the value each time by one to prevent the infinite loop. The loop was executed until the data value equalled the number 10.

Java do-while loop

With the do-while loop, the body of the loop is executed once before the expression is checked.

The syntax of the do-while loop:

 do {
    // body of loop
 } while(expression);

Here, the body of the loop gets executed first. Then, the expression is evaluated, and if it evaluates to true, the loop’s body gets executed again. This process continues until the expression evaluates to false.

The flow of the do-while loop looks like this:

java do while loop


Example of the Java do-while loop:

class Test {
    
  public static void main(String[] args) {
    int data = 5;
        
    do {
      System.out.println("Data: " + data);
      data++;
    } while (data < 10);
      
    System.out.println("Code after the do-while loop...");
  }
}
Output: Data: 5 Data: 6 Data: 7 Data: 8 Data: 9 Code after the do-while loop…
 
Here, we also needed to increase the value of the data in each iteration to prevent an infinite loop.

Inside the curly braces, a loop body will be executed first at the beginning, regardless of whether the expression is false or true. The expression will be tested for the first time after the first execution of the loop bodyIf the value is true, the process gets repeated until it becomes false. If the value is false in the first iteration, the loop will terminate.

Take a look at another example:

class Test {
    
  public static void main(String[] args) {
    int data = 5;
        
    do {
      System.out.println("Data: " + data);
      data++;
    } while (data < 5);
      
    System.out.println("Code after the do-while loop...");
  }
}
Output: Data: 5 Code after the do-while loop…
 
Here, you see that although the result of the expression is equal to false at the beginning, the body of the loop is executed once. It’s because the expression testing comes after the loop’s body.

That’s it!

Leave a Reply

Your email address will not be published.