1. Introduction
Java provides two loop constructs based on a single boolean condition without a built-in counter: the while loop and the do-while loop. Both repeat a block of code as long as a condition remains true, but they differ in when that condition is checked.
Cricket analogy: A bowler keeps bowling overs as long as the captain hasn't declared or the target isn't reached, but whether the review happens before or after the over decides if a wasted over is possible, just like while versus do-while.
The while loop is entry-controlled (pre-test): the condition is checked before the loop body runs, so the body may execute zero times. The do-while loop is exit-controlled (post-test): the condition is checked after the body runs, guaranteeing the body executes at least once.
Cricket analogy: The while loop is like an umpire checking the light meter before every over so an over is skipped entirely if it's too dark, while the do-while is like bowling one over first and only then checking if light permits another, guaranteeing at least one over.
2. Syntax
while loop
while (condition) {
// loop body — may run zero times
}do-while loop
do {
// loop body — runs at least once
} while (condition); // note the mandatory trailing semicolon3. Explanation
In a while loop, if the condition evaluates to false on the very first check, the body never executes at all. This makes while loops suitable when it's uncertain whether the body should run even once, such as validating user input before any processing.
Cricket analogy: If the target score is already zero runs needed when the batting side walks out, like a Duckworth-Lewis washout scenario, the while loop's innings never even begins, avoiding a pointless over being bowled.
In a do-while loop, the body runs first, and the condition is evaluated afterward to decide whether to repeat. Because the check happens after execution, the body is guaranteed to run at least one time, which is useful for menu-driven programs or input validation loops that must present a prompt before checking a response.
Cricket analogy: A do-while loop is like a team batting one mandatory over first before the umpire checks if rain has stopped play, guaranteeing at least one over is bowled before any condition halts the match.
Exam trap: forgetting the trailing semicolon after while (condition) in a do-while loop is a compile error — unlike the while loop and for loop, the do-while statement must end with ; after the condition.
Both loops support break and continue for flow control, and both can become infinite loops if the condition never becomes false (e.g., while (true) { }).
4. Example
public class WhileDoWhileDemo {
public static void main(String[] args) {
// while loop: entry-controlled
int count = 5;
while (count < 3) {
System.out.println("while body: " + count);
count++;
}
System.out.println("After while loop, count = " + count);
// do-while loop: exit-controlled, runs at least once
int n = 5;
do {
System.out.println("do-while body: " + n);
n++;
} while (n < 3);
System.out.println("After do-while loop, n = " + n);
}
}5. Output
After while loop, count = 5
do-while body: 5
After do-while loop, n = 66. Key Takeaways
- while is entry-controlled: the condition is checked before the body runs, so it may execute zero times.
- do-while is exit-controlled: the body runs first and the condition is checked after, guaranteeing at least one execution.
- do-while requires a trailing semicolon after the condition — omitting it is a compile error.
- Both loops can become infinite if the condition never turns false.
- do-while is well suited to input-validation or menu loops that must run once before checking.
- Both support break and continue for controlling iteration flow.
Practice what you learned
1. How many times does a while loop's body execute if its condition is false on the first check?
2. What guarantees does a do-while loop provide compared to a while loop?
3. What is required at the end of a do-while loop's condition in Java?
4. Which loop type is considered 'entry-controlled' (pre-test)?
5. Which scenario best suits a do-while loop?
Was this page helpful?
You May Also Like
for Loop in Java
Understand Java's classic three-part for loop and the enhanced for-each loop for iterating over arrays and collections concisely.
break and continue in Java
Understand how break and continue alter loop execution in Java, including labeled break/continue for controlling nested loops directly.
if-else in Java
Learn how Java's if-else statement controls program flow with conditional branching, including if, if-else, else-if ladders, and nested if statements.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics