In Part 1, we explored the while loop and learned that it checks its condition before executing the loop body. This means a while loop may execute zero times if the condition is initially false.
But what if you want the loop body to execute at least once, regardless of the condition?
That's exactly what the do-while loop is designed for.
In this article, we'll learn how the do-while loop works, understand its syntax, explore compiler rules, and discuss common interview questions and beginner mistakes.
What Is the do-while Loop?
A do-while loop executes the loop body first, and then checks the condition.
Because the condition is evaluated after the body executes, the loop body always runs at least once.
Syntax
do {
// loop body
} while (condition);
Notice the semicolon (;) after the while condition.
It is mandatory.
How the do-while Loop Works
The execution flow is:
- Execute the loop body.
- Evaluate the condition.
- If the condition is
true, repeat the loop. - Otherwise, exit the loop.
Unlike the while loop, the body is guaranteed to execute once.
Your First do-while Program
int count = 1;
do {
System.out.println(count);
count++;
} while (count <= 5);
Output
1
2
3
4
5
Why Use a do-while Loop?
Suppose you want to display a menu to the user.
The menu should appear at least once, even before any input is received.
boolean continueProgram = false;
do {
System.out.println("Welcome!");
} while (continueProgram);
Output
Welcome!
Even though the condition is false, the body executes once.
while vs do-while
boolean condition = false;
Using while
while (condition) {
System.out.println("Hello");
}
Output
No output
Using do-while
do {
System.out.println("Hello");
} while (condition);
Output
Hello
This is the biggest difference between the two loops.
Rule 1: The Condition Must Be boolean
Correct
int count = 1;
do {
count++;
} while (count <= 5);
Incorrect
do {
} while (1);
Compile-time error
incompatible types:
int cannot be converted to boolean
Java never converts integers into boolean values.
Rule 2: Curly Braces Are Optional
For a single statement, braces may be omitted.
int count = 1;
do
System.out.println(count++);
while (count <= 3);
Output
1
2
3
Although valid, braces improve readability.
Multiple Statements Need Braces
Incorrect
int count = 1;
do
System.out.println(count);
count++;
while (count <= 3);
Only the first statement belongs to the loop.
Correct
int count = 1;
do {
System.out.println(count);
count++;
} while (count <= 3);
Rule 3: Variable Declarations Need Braces
Incorrect
do
int number = 10;
while (true);
Compile-time error
variable declaration not allowed here
Correct
do {
int number = 10;
} while (false);
Rule 4: The Semicolon Is Mandatory
Incorrect
do {
System.out.println("Hello");
} while (false)
Compile-time error
The compiler expects a semicolon.
Correct
do {
System.out.println("Hello");
} while (false);
Empty Statement
A semicolon is a valid statement.
do;
while (true);
This is a valid infinite loop with an empty body.
Although legal, it is rarely useful.
Nested do-while
Nested loops are allowed.
int outer = 1;
do {
int inner = 1;
do {
System.out.println(outer + " " + inner);
inner++;
} while (inner <= 2);
outer++;
} while (outer <= 2);
Output
1 1
1 2
2 1
2 2
Unreachable Statement Rule
Java reports an error whenever it knows that some code can never execute.
Case 1
do {
} while (true);
System.out.println("Done");
Compile-time error
unreachable statement
The compiler knows the loop never ends.
Case 2
do {
System.out.println("Hello");
} while (false);
System.out.println("Done");
Output
Hello
Done
This surprises many beginners.
Unlike a while loop, the body executes before checking the condition.
Effect of final
Example
final int start = 10;
final int end = 20;
do {
} while (start < end);
System.out.println("Done");
Compile-time error
unreachable statement
Why?
The compiler replaces
start < end
with
10 < 20
which becomes
true
The compiler now knows the loop is infinite.
Without final
int start = 10;
int end = 20;
do {
} while (start < end);
System.out.println("Done");
This compiles successfully because the compiler cannot determine the condition during compilation.
Execution Trace
int count = 1;
do {
System.out.println(count);
count++;
} while (count <= 3);
| Iteration | count before | Output | count after | Condition |
|---|---|---|---|---|
| 1 | 1 | 1 | 2 | true |
| 2 | 2 | 2 | 3 | true |
| 3 | 3 | 3 | 4 | false |
Common Beginner Mistakes
Forgetting the Semicolon
Incorrect
do {
} while (false)
Always remember the trailing semicolon.
Forgetting to Update the Loop Variable
int count = 1;
do {
System.out.println(count);
} while (count <= 5);
Result
Infinite loop.
Using Assignment Instead of Comparison
boolean running = false;
do {
} while (running = true);
The assignment returns true, causing an infinite loop.
Using Non-Boolean Conditions
Incorrect
do {
} while (100);
Only boolean expressions are allowed.
Best Practices
- Use
do-whilewhen the body must execute at least once. - Always update the loop variable.
- Prefer braces, even for a single statement.
- Keep loop conditions simple.
- Avoid accidental infinite loops.
- Never forget the mandatory semicolon.
Interview Questions
What is the main difference between while and do-while?
A while loop checks the condition first.
A do-while loop executes the body first.
Can a do-while loop execute zero times?
No.
It always executes at least once.
Is the semicolon mandatory?
Yes.
It is part of the syntax.
Why does do-while(false) execute once?
Because the condition is evaluated after the body executes.
Why can adding final produce an unreachable statement error?
Because the compiler can evaluate the condition at compile time and determine that the loop never exits.
Quick Memory Trick 🧠
Remember DO:
- D → Do the work first.
- O → Only then check the condition.
Or simply remember:
"do first, decide later."
while vs do-while
| Feature | while |
do-while |
|---|---|---|
| Condition checked | Before execution | After execution |
| Minimum executions | 0 | 1 |
| Semicolon required | No | Yes |
| Best use case | Unknown iterations | Must execute once |
Key Takeaways
- A
do-whileloop executes the body before checking the condition. - It always executes at least once.
- The condition must evaluate to a boolean value.
- The semicolon after
while(condition)is mandatory. - Curly braces are optional for a single statement but recommended.
-
do-while(false)executes exactly one iteration. -
finalvariables may allow the compiler to detect infinite loops and report unreachable statements. - Use
do-whilewhen at least one execution is required.
If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.
Happy Coding!
Top comments (0)