Java Do-While Loop
Syntax, Flow, and Examples

In Java, unlike the while loop, which checks the condition before executing the code block, the do-while loop guarantees at least one execution—no matter what the condition says. It's a perfect fit when your logic requires the body to run before validation kicks in.

What is a Do-While Loop?

A do-while loop in Java is a control flow statement that executes a block of code at least once, and then continues to execute it as long as the specified condition is true.

Syntax of Do-While Loop

do {
    // Code to execute
} while (condition);

Note: The semicolon after the while condition is required. This often trips up beginners, so keep an eye on it!

Step-by-Step Example

public class Main {
    public static void main(String[] args) {
        int count = 1;
        do {
            System.out.println("Count is: " + count);
            count++;
        } while (count <= 3);
    }
}
Count is: 1
Count is: 2
Count is: 3

How It Works

Here’s what happens line-by-line:

  • count starts at 1.
  • The do block runs and prints “Count is: 1”.
  • count increments to 2, and the loop condition (count <= 3) is checked.
  • Since it's still true, the block runs again. This continues until count becomes 4.
  • At count = 4, the condition fails, and the loop exits.

Why Use a Do-While Loop?

The do-while loop shines in scenarios where the loop body must execute at least once before the condition is checked. For example:

import java.util.Scanner;

public class LoginPrompt {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String password;
        
        do {
            System.out.print("Enter your password: ");
            password = scanner.nextLine();
        } while (!password.equals("admin123"));
        
        System.out.println("Access granted.");
    }
}
Enter your password: user1
Enter your password: hello
Enter your password: admin123
Access granted.

This loop ensures that the user is always prompted at least once. The moment the correct password is entered, it breaks out of the loop.

Edge Case: False Condition from Start

public class Main {
    public static void main(String[] args) {
        int x = 100;
        do {
            System.out.println("This will run once even if condition is false.");
        } while (x < 50);
    }
}
This will run once even if condition is false.

Unlike while or for loops, a do-while will not skip execution—even if the condition is false from the beginning.

Best Practices

  • Use do-while when the logic demands at least one execution.
  • Make sure the loop condition eventually becomes false to avoid infinite loops.
  • Use meaningful loop conditions—especially in user input scenarios.

Do-While vs While: Quick Comparison

Aspectdo-whilewhile
Condition checkAfter executionBefore execution
Minimum executions10
Use caseUser prompt, menusLoops with known condition upfront

Conclusion

The Java do-while loop is more than just a variation of while; it’s a purposeful construct tailored for specific use cases. When you're certain that your block of code needs to run at least once, do-while loop is the one you have to use.

QUIZ

Question 1:How many times will the following code execute the `System.out.println` line?
int count = 5;
do {
    System.out.println("Run");
} while (count < 0);

Question 2:A semicolon is not required after the while condition in a Java do-while loop.

Question 3:Which of the following are true about Java do-while loops?

Question 4:What will the following program output?
int i = 1;
do {
    System.out.print(i + " ");
    i++;
} while (i <= 3);

Question 5:Do-while loops in Java are better suited than while loops for validating user input at least once.

Question 6:Which of these use cases are ideal for a Java do-while loop?