do-while loop in Java with examples

Java supports loops, which are used to repeatedly run a block of code until a specific condition is met. This is one of Java's key features. The do-while loop is a frequently used construct that offers a flexible and effective mechanism for iterative programming among the various types of loops available in Java.

The do-while loop will be thoroughly explained in this article, along with its syntax and guidelines for using it correctly in Java programs. Finally, we'll talk about some examples of do-while loops.

What is a do-while loop in Java?

Java's do-while loop is a type of loop that repeatedly runs a block of code up until a predetermined condition is satisfied. The only difference between a do-while loop and a while loop in Java is that the condition is evaluated at the end of the loop rather than the beginning. This indicates that the code contained within the loop will always be executed at least once, even if the initial condition is false.

Java do-while loop syntax

The general form, or syntax, of the "do-while" loop in Java is as follows:

do {
   // body of the do-while loop
} while (condition);

In the above general form of the "do-while" loop, the "condition" is the expression that is evaluated after each loop iteration. If the condition is met, the loop continues to run. If the condition is false, the loop ends and program control returns to the statement after the do-while loop.

Important: Because condition is at the bottom or after the loop's body (the block of code written in the body of the loop) will be executed first, then its "condition" will be evaluated, and if the condition evaluates to true, the loop's body will be executed again. This process is repeated until the condition is determined to be false.

Java do-while loop example

Consider the following Java program as an example demonstrating the "do-while" loop:

Java Code
public class DoWhileExample {
    public static void main(String[] args) {
        int num = 1;

        do {
            System.out.println(num);
            num++;
        } while (num <= 5);
    }
}
Output
1
2
3
4
5

The program sets the integer variable num to 1. The "do" keyword kicks off the "do-while" loop. The code block that follows the "do" keyword is run at least once. The current value of num is printed to the output console within the loop body using the System.out.println() method. The num++ operator increases the value of num by one.

In parentheses, the "while" keyword specifies the loop condition. The loop will continue to execute as long as num is less than or equal to 5. When the value of num exceeds 5, the loop is terminated and program execution is completed.

Because the body of the loop executes before the "condition," the body of the loop always executes once, even if the "condition" evaluates to "false" at first. As an example:

Java Code
public class DoWhileExample {
    public static void main(String[] args) {
        int num = 1;

        do {
            System.out.println(num);
            num++;
        } while (num >= 5);
    }
}
Output
1

Before we wrap up our discussion of Java's "do-while" loop, I'd like to include one more example that prompts the user to enter a positive number, and if the user enters a positive number, the program prints the number back on the output console. If not, the program will continue to accept input until the user enters a positive number.

Java Code
import java.util.Scanner;

public class DoWhileExample {
    public static void main(String[] args) {
        Scanner inpt = new Scanner(System.in);
        int num;

        do {
            System.out.print("Enter a positive integer: ");
            num = inpt.nextInt();
        } while (num <= 0);

        System.out.println("You entered: " + num);
    }
}
Output
Enter a positive integer: -3
Enter a positive integer: -5
Enter a positive integer: 23
You entered: 23

Java Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!