🔀 Control Flow · Beginner

do-while loop in Java

Body runs at least once.

🧩 The mysteryA menu has to appear before the user can choose "Quit". But a while loop asks first and acts later. You need a loop that acts first.

Act first, ask later

A do-while loop runs its body first and checks the condition afterwards. So the body always runs at least once, even if the condition is false from the start.

int tries = 0;
do {
    tries++;
} while (tries < 3);

The shape (mind the semicolon)

The syntax is do { ... } while (condition); and the semicolon at the end is required. Leave it out and the code doesn't compile. Also remember scope: the condition sits outside the braces.

int x = 0;
do {
    x++;
} while (x < 3); // <- this ; is required
System.out.println(x);
🔮 Predict it

Your turn

n starts at 50, and 50 < 20 is false. What prints?

int n = 50;
do {
    System.out.println(n);
    n += 10;
} while (n < 20);
  1. Nothing is printed
  2. 50
  3. 50 60
Show the answer

50. The body runs once before any check, printing 50 and making n 60. Then 60 < 20 is false, so the loop stops.

🔮 Predict it

Trace it

What prints?

int i = 1;
do {
    i *= 3;
} while (i < 10);
System.out.println(i);
  1. 9
  2. 10
  3. 27
Show the answer

i becomes 3 (3 < 10, repeat), then 9 (9 < 10, repeat), then 27. Now 27 < 10 is false and the loop ends. The check comes *after* each pass.

⚠️ The trap

The body's variables are gone

A variable declared inside the body dies at its closing brace, and the while (...) condition comes after that brace. So the condition can't see it: cannot find symbol. Declare it before the do instead.

do {
    String answer = ask();
} while (answer.equals("again")); // error
 
String answer;            // fix:
do {                      // declare
    answer = ask();       // outside
} while (answer.equals("again"));
🤔 Think first

Which job fits?

Summing an array, or showing a menu and repeating until the user picks Quit: which is the natural do-while?

Think about it, then reveal the answer

The menu. You must show it at least once before you know whether to repeat: exactly do-while's "run first, check later" shape. Summing an array might involve zero elements, so a check-first loop fits better.

💼 In the real world

In real projects

do-while shines for "do, then decide" logic: show a menu, read input until it's valid, try to connect and retry if it failed. It's the least common of Java's loops, but in those spots it removes the awkward "run it once before the loop" duplication.

Key takeaways

  1. The body always runs at least once
  2. The condition is checked AFTER each pass
  3. Syntax: do { ... } while (condition); (note the semicolon)
  4. Variables declared in the body are not visible in the condition

💡 Like tasting a dish and then deciding whether to add more salt: you always taste at least once.

🤯 Did you know?

C programmers wrap multi-line macros in do { ... } while (0): a loop that runs exactly once. It exists purely so the macro behaves like a single statement after an if.

Practice questions

What does this print?

int n = 10;
do {
    System.out.println(n);
    n++;
} while (n < 5);
  1. Nothing is printed
  2. 10
  3. 10 11
  4. 5
Check your answer

10. The body runs once and prints 10, then 11 < 5 is false, so the loop stops.

What does this print?

int i = 0;
do {
    i += 2;
} while (i < 7);
System.out.println(i);
  1. 6
  2. 7
  3. 8
  4. 10
Check your answer

8. i becomes 2, 4, 6 and then 8. After the pass that makes it 8, 8 < 7 is false and the loop ends.

Next: the for loop. Three parts in one line, and the most famous bug in programming: off by one.