Writing while Loops
The
whileanddo-whileloops repeat a block of code as long as a boolean condition holds. They are the right tool when the number of iterations is not known in advance.
The while Loop
A while loop evaluates its condition before each iteration. If the condition is false on the very first check, the body never runs:
javaint count = 0;
while (count < 5) {
System.out.println("count = " + count);
count++;
}
// Prints: count = 0, 1, 2, 3, 4Execution flow:
- Evaluate the condition
- If
true, execute the body and go to step 1 - If
false, exit the loop
Zero-Iteration Example
Because the condition is checked first, a while loop can execute zero times:
javaint x = 10;
while (x < 5) { // false immediately
System.out.println(x);
}
System.out.println("Done"); // runs immediatelyThis is a key difference from do-while.
The do-while Loop
A do-while loop evaluates its condition after each iteration. The body always executes at least once:
javaint count = 0;
do {
System.out.println("count = " + count);
count++;
} while (count < 5);
// Prints: count = 0, 1, 2, 3, 4Note the semicolon after the closing condition — it is required.
Guaranteed One-Time Execution
javaint input = 10;
do {
System.out.println("Body always runs once: " + input);
} while (input < 5); // condition is false, but body already ran
// Output: Body always runs once: 10Comparing while vs do-while
| Feature | while |
do-while |
|---|---|---|
| Condition check | Before each iteration | After each iteration |
| Minimum iterations | 0 | 1 |
| Body runs when condition starts false | No | Yes (once) |
| Trailing semicolon required | No | Yes |
Common Use Cases
Reading user input until valid:
javaScanner scanner = new Scanner(System.in);
String input;
do {
System.out.print("Enter a non-empty string: ");
input = scanner.nextLine();
} while (input.isEmpty());Waiting for a condition to become true:
javaint retries = 0;
boolean success = false;
while (!success && retries < 3) {
success = attemptConnection();
retries++;
}Infinite Loop Risk
If the condition never becomes false, the loop runs forever. This is sometimes intentional (server event loops) but is usually a bug:
java// BUG: count is never incremented
int count = 0;
while (count < 5) {
System.out.println(count);
// forgot: count++;
}Common causes of unintentional infinite loops:
| Cause | Example |
|---|---|
| Forgetting to update the loop variable | while (i < 10) { doWork(); } |
| Updating the wrong variable | while (i < 10) { j++; } |
Condition can never be false |
while (true) { } without a break |
Intentional Infinite Loops
An explicit while (true) loop is acceptable when an internal break or return provides the exit:
javawhile (true) {
String line = readLine();
if (line == null) {
break; // exit when input is exhausted
}
process(line);
}Variable Scope Inside Loops
Variables declared inside a loop body are created and destroyed each iteration:
javawhile (condition) {
int temp = compute(); // new variable each iteration
System.out.println(temp);
}
// temp is not accessible hereVariables declared before the loop are accessible inside it and retain their value between iterations:
javaint sum = 0;
int i = 1;
while (i <= 10) {
sum += i;
i++;
}
System.out.println(sum); // 55Key Rules to Remember
whilechecks the condition before the body — zero iterations are possibledo-whilechecks the condition after the body — the body always runs at least once- The semicolon after
} while (condition)is mandatory indo-while - The condition must be a
booleanexpression - Always ensure the loop variable moves toward the exit condition to avoid infinite loops