Java Notes
A do-while loop runs its body once before checking the condition. Use it for menus and actions that must happen at least once.
Sometimes you need code to run before you know whether it should repeat. A login prompt, a menu, a dice roll — all of these must happen at least once before the user decides to continue or stop. That is exactly what the do-while loop is for.
What is an exit-controlled loop?
Every loop has one question: *should I run again?*
- A
whileloop checks the question before running the body. If the answer is "no" from the start, the body never runs. - A
do-whileloop checks the question after running the body. So the body always runs at least once, no matter what.
This "check after" behaviour is called exit-controlled, because the exit decision happens at the end, not the beginning.
Syntax
do {
// body — runs first, condition checked after
} while (condition);Two things students forget:
- The semicolon after
while (condition)— it is required. Without it, Java gives a compile error. - The body still needs a variable that changes, otherwise the condition never becomes false and you get an infinite loop.
Your first do-while
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
int count = 1;— counter starts at 1.do {— Java enters the body immediately without checking anything.System.out.println(...)— prints the current count.count++— increases count by 1 each time.} while (count <= 5);— after each body run, Java checks: is count still ≤ 5? If yes, repeat. If no, stop.
When count reaches 6, the condition 6 <= 5 is false, so the loop stops. Total prints: 5.
do-while vs while — the key difference
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
value is 10, which is not less than 5. The while loop skips entirely — zero prints. The do-while loop runs the body once first, then checks the condition, finds it false, and stops — one print.
| Loop | Condition checked | Body runs minimum |
|---|---|---|
while | Before body | 0 times |
do-while | After body | 1 time |
When to prefer do-while: any situation where the action must happen before the user or program can decide whether to repeat — menus, retry prompts, input validation.
Common mistake: forgetting the semicolon
// WRONG — compile error
do {
System.out.println("hello");
} while (true) // missing semicolon
// CORRECT
do {
System.out.println("hello");
} while (true);Java sees } while (true) without a semicolon and does not know the statement is complete. Always end do-while with a semicolon.
Common mistake: infinite do-while
If the variable that the condition tests is never updated inside the body, the condition stays true forever.
Real use: menu-driven program
Menu programs are the classic do-while use case. The menu must appear before the user types anything.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
int choice;— declared outside the loop so the conditionchoice != 0can see it.do {— menu prints before Java knows choice's value. This is the whole reason to use do-while here.scanner.nextInt()— reads what the user typed as an integer.} while (choice != 0);— if the user entered 0, exit. Otherwise, show the menu again.
Note: this program reads from keyboard (stdin). Run it in your own terminal, not a browser-based output window.
Practice task
Task: Write a number guessing game. Set a secret number (e.g., 42). Use a do-while loop to keep asking the user to guess until they get it right. Print "Too low", "Too high", or "Correct!" each round.
Hint: You need a variable for the user's guess declared before the loop. The condition should repeat while the guess is not equal to the secret number.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: Try entering values less than 42, greater than 42, and finally 42 itself. Notice the loop always runs at least once — even before you type anything.
Continue the lesson path