do-while loop in Java
Body runs at least once.
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);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);Nothing is printed5050 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.
Trace it
What prints?
int i = 1;
do {
i *= 3;
} while (i < 10);
System.out.println(i);91027
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 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"));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 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
- The body always runs at least once
- The condition is checked AFTER each pass
- Syntax: do { ... } while (condition); (note the semicolon)
- 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.
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);- Nothing is printed
- 10
- 10 11
- 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);- 6
- 7
- 8
- 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.