do...while Loop in Java - Flowchart & Syntax (With Examples)

The do...while loop in Java is similar to the while loop, but with one key difference: it checks its condition after executing the loop body, guaranteeing that the code runs at least once.

Syntax

do {
    // code to repeat
} while (condition);

Basic Example

int i = 1;
do {
    System.out.println(i);
    i++;
} while (i <= 5);
// prints 1 2 3 4 5

Flowchart of do...while

The flow starts with the loop body, runs it once, then checks the condition. If the condition is true, control goes back to the loop body; if it's false, the loop exits. This makes do...while an exit-controlled loop, unlike for and while, which are entry-controlled.

Executing at Least Once

Even if the condition is false from the very beginning, the loop body still runs one time before the condition is checked.

int n = 10;
do {
    System.out.println("This runs once even though n > 5 is false");
} while (n > 5 && n < 5); // condition is false immediately

Common Use Case: Menu-Driven Programs

The do...while loop is frequently used for menu-driven programs, where you want to show the menu at least once before checking whether the user wants to continue.

int choice;
do {
    System.out.println("1. Add  2. Delete  3. Exit");
    choice = getUserChoice();
} while (choice != 3);
  • do...while always runs its body at least once, regardless of the condition
  • The condition is checked after the loop body, not before
  • A semicolon is required after the closing while(condition), unlike a regular while loop
Choose do...while whenever your logic requires the code to run at least once before deciding whether to repeat — like prompting a user for input.

Master Java with Uncodemy

Hands-on training, live projects, and placement support in our Java Programming Course.

Explore the Course