Java do while loop

Summary: in this tutorial, you will learn how to use the Java do while loop to execute a block of code repeated as long as a specified condition is true.

Introduction to Java do while loop

Java do while loop executes a block of code as long as a condition remains true:

do {
   // Code to be executed
} while(condition);Code language: Java (java)

In this syntax, the condition is a boolean expression that evaluates to true or false.

Unlike the while loop, the do while loop evaluates the condition at the end of each iteration. If the condition is true, the loop continues. If the condition become false, the loop will terminate.

Therefore, inside the loop, you need to make some changes so that the condition will become false at some point. Otherwise, you’ll have an indefinite loop.

Because the do while loop evaluates the condition at the end of each iteration, the do while loop is also known as a posttest loop.

Also, the do while loop always runs the first iteration whether the condition is true or false. It’s useful in scenarios where you want to execute the first iteration and test the condition. For example, you want to prompt the user for input and continue checking that input until the user issues a quit signal.

Java do while loop examples

Let’s take some examples of using the do while loops.

1) A simple do while loop example

The following program uses a do while loop to display integer numbers from 1 to 5:

public class App {
    public static void main(String[] args) {
        int count = 0;
        do {
            count++;
            System.out.println(count);
        } while (count < 5);
    }
}Code language: Java (java)

Output:

1
2
3
4
5Code language: Java (java)

How it works.

  • First, declare a variable count and initialize its value to 0.
  • Second, display the value of the count variable as long as the count is less than five. In each iteration, increase the value of the count variable by one. When the count reaches 5, the loop exits.

2) Using the do while loop to calculate the sum of a sequence of integers

The following example illustrates how to use the do while loop to calculate the sum of all integers from 1 to 100:

public class App {
    public static void main(String[] args) {
        
        int total = 0;
        int count = 1;

        do {
            total += count;
            count++;
        } while (count <= 100);

        System.out.println(total);
    }
}Code language: Java (java)

Output:

5050Code language: Java (java)

How it works.

First, declare the total and count variables and initialize their values to 0 and 1 respectively.

Second, add the value of the count to the total as long as the count is less than or equal to 100. Increase the value of the count by one in each iteration.

Third, display the total.

Summary

  • Use Java do while loop to execute a block of code repeatedly as long as a condition remains true, and evaluate the condition at the end of each iteration.