The do-while loop is another repetition structure in Java. It is similar to the while loop, but there is one important difference: the do-while loop executes its body first and checks the condition afterward.
Why Do We Need a do-while Loop?
Some tasks need to happen at least once before the program decides whether they should happen again. A menu-driven application is a good example. The program should display the menu first, allow the user to make a choice, and only then decide whether to display the menu again.
A do-while loop guarantees that its body executes at least once because the condition is checked after the first execution.
Real-World Analogy
Imagine a restaurant where a waiter asks for your order. The waiter must ask you at least once before deciding whether to ask for another order.
Do
Ask for the order
While the customer wants to continue
The action happens first. The decision to repeat happens afterward. This is exactly how a do-while loop behaves.
Basic Syntax
do {
// code to execute
} while (condition);
Notice the semicolon after the condition. This semicolon is part of the do-while syntax and is an important detail for beginners to remember.
Simple Example
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
The program first prints 1 and increases i. Only after that does Java check whether i <= 5. If the condition is true, the loop repeats.
How the do-while Loop Works
- Java enters the do block.
- The statements inside the block execute.
- Java evaluates the while condition.
- If the condition is true, the loop body executes again.
- If the condition is false, the loop stops.
Remember: In a do-while loop, the body comes first and the condition comes afterward.
The Important Difference from while
The most important difference between while and do-while is when the condition is checked.
| Feature | while | do-while |
|---|---|---|
| Condition check | Before the body | After the body |
| Minimum executions | Zero | One |
| Best suited for | When the body may not need to execute. | When the body must execute at least once. |
Example: Condition Is False Initially
This example clearly shows why a do-while loop is different from a while loop.
int number = 10;
do {
System.out.println(number);
number++;
} while (number < 5);
The condition number < 5 is false from the beginning. However, the program still prints 10 because the body executes before the condition is checked.
Comparing with while
int number = 10;
while (number < 5) {
System.out.println(number);
number++;
}
In this case, nothing is printed because the condition is checked before the body. Since 10 is not less than 5, Java skips the loop completely.
This is the key distinction: while can execute zero times, but do-while executes at least once.
Example: Menu-Driven Program
int choice = 1;
do {
System.out.println("1. Add Student");
System.out.println("2. View Student");
System.out.println("3. Exit");
choice++;
} while (choice <= 3);
A menu is a classic situation where do-while can be useful. The menu needs to appear before the program can decide whether it should be displayed again.
Example: Simple Validation
int marks = 120;
do {
System.out.println("Checking marks...");
marks++;
} while (marks < 100);
The body executes once even though the condition is already false. In real applications, validation logic can use this structure when an input must be requested or processed at least once.
Updating the Loop Variable
Just like other loops, a do-while loop must normally have a clear path toward termination.
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
The i++ update moves the variable toward the stopping condition. Without a suitable update, the loop may continue indefinitely.
Common Beginner Mistakes
- Forgetting the semicolon after the while condition.
- Assuming the condition is checked before the first execution.
- Forgetting to update the variable controlling the condition.
- Using do-while when the body should be allowed to execute zero times.
- Creating an infinite loop because the condition never becomes false.
Best Practices
- Use do-while when at least one execution is logically required.
- Keep the termination condition easy to understand.
- Make the update or state change obvious.
- Use a while loop instead when the body may legitimately need to execute zero times.
- Be especially careful with input and menu loops to ensure there is a clear exit path.
Interview Insight
A common interview question is: “Which loop guarantees at least one execution of its body?”
The answer is the do-while loop because its condition is checked after the loop body has executed.
Quick Revision
| Concept | Key Point |
|---|---|
| do-while | Repeats code while a condition remains true. |
| First execution | Always occurs before the condition is checked. |
| Condition | Checked after each execution. |
| Minimum executions | At least one. |
| Semicolon | Required after the while condition. |
The do-while loop is ideal when an operation must happen at least once before Java decides whether to repeat it. With the three basic loop structures now covered—for, while, and do-while—the next step is the enhanced for loop, which provides a simpler way to traverse arrays and collections.
