Java Tutorial Series: This lesson is part of our comprehensive Complete Java Tutorial: Learn Java from Beginner to Advanced.

Do-While Loop in Java: Syntax, Examples and When to Use It

What Is a do-while Loop?

The do-while loop executes a block and then checks its condition. Because the condition is evaluated after the body, the body always executes at least once.

Basic Syntax

The semicolon after the condition is required.

Execution Flow

Java enters the do block first, executes its statements, evaluates the condition and either repeats or exits. This makes do-while different from while, which checks first.

Simple Example

int count = 1;
do {
    System.out.println(count);
    count++;
} while (count <= 5);

Why Use do-while?

It is useful when an operation must happen at least once before the program can decide whether to repeat it. Menus, retry prompts and initial input requests are common examples.

int option;
do {
    System.out.println("1. Continue");
    System.out.println("2. Exit");
    option = 2;
} while (option != 2);

A real application would obtain option from an input source.

do-while vs while

If the initial condition is false, while skips its body. do-while still runs once. Choose based on this behavioral difference rather than on personal preference.

break and continue

break can exit a do-while immediately. continue causes control to proceed toward the next condition check. Carefully verify state updates when using continue.

Retry Example

int attempts = 0;
do {
    attempts++;
    System.out.println("Attempt " + attempts);
} while (attempts < 3);

Common Mistakes

Forgetting the semicolon after while(condition), using a condition that never becomes false and misunderstanding the guaranteed first execution are common errors.

Debugging

Check the state after the first body execution, then verify whether the condition correctly represents the decision to repeat.

Best Practices

Use do-while when one execution is required, keep the exit condition clear and avoid using it simply because it is shorter than another loop structure.

Practice Problems

Build a menu, retry counter, sentinel-controlled input loop, simple game loop and a program that repeatedly asks for valid input.

Frequently Asked Questions

Does do-while execute at least once? Yes.

When is the condition checked? After the body.

What is the main difference from while? while checks before execution; do-while checks after.

Conclusion

do-while is a valuable control-flow structure for at-least-once workflows. It completes the core Java loop family alongside for and while.

Learn More

  • Java Syntax
  • Java Variables
  • Java Data Types
  • Java Operators
  • Java Input and Output
  • Java Methods
  • Java Arrays and Collections

Continue Learning Java

After this topic, continue through the Java control-flow path by practicing the next related statement or loop. Build small programs rather than only reading examples.

Deeper Practical Coverage

Guaranteed First Execution

The defining feature of do-while is that the body runs before the condition is evaluated. This makes it a natural fit for operations that must happen once before the program can decide whether to repeat.

A menu is a classic example. The program displays the menu at least once, reads an option, performs an action, and then decides whether to display the menu again.

do {
    displayMenu();
    option = readOption();
    handleOption(option);
} while (option != 0);

This pattern is easy to explain because the first menu display is guaranteed.

Validation and Retry

A do-while is also useful for bounded retry logic:

int attempts = 0;
boolean valid;
do {
    attempts++;
    valid = validateInput();
} while (!valid && attempts < 3);

The attempt happens first, then the decision to repeat is made.

Choosing Between while and do-while

Ask a simple question: should the body be allowed to execute zero times? If yes, while may be more appropriate. If the first execution is mandatory, do-while is a natural fit.

Termination and Safety

A do-while can still become infinite. Make sure the state used by the condition is updated inside the body and that all possible paths can eventually reach the stopping condition or an intentional break/return.

Extended Practice Set

  • Build a repeating menu.
  • Implement three-attempt validation.
  • Compare while and do-while for the same task.
  • Create a retry loop with a success flag.
  • Demonstrate guaranteed first execution.

Java Language and Version Note

The Java platform documentation groups control flow into decision-making, looping, and branching constructs. Modern Java has also expanded switch with expression forms, arrow rules, and pattern-oriented capabilities. When publishing this article, the website should state the Java version when an example depends on a newer language feature. Core syntax such as if, if-else, for, while, do-while, break, continue, and traditional switch remains foundational, while newer forms should be presented as version-aware additions.