while loop in Java
Condition checked before each iteration; may run zero times.
Repeat while true
A while loop repeats its body as long as its condition is true: check, run, check, run... Something inside the body must eventually make the condition false, or the loop never ends.
int n = 3;
while (n > 0) {
System.out.println(n);
n--;
} // prints 3, 2, 1Checked BEFORE each pass
The condition is tested before every pass, including the very first. If it's false from the start, the body runs zero times. Here 10 < 5 is false immediately, so only end prints.
int count = 10;
while (count < 5) {
System.out.println(count);
count++;
}
System.out.println("end");Trace it
What prints?
int i = 2;
while (i < 30) {
i *= 2;
}
System.out.println(i);16303264
Show the answer
i goes 2, 4, 8, 16, 32. At 16, 16 < 30 is still true, so one more pass doubles it to 32. Then 32 < 30 fails. The condition is only checked *between* passes, so the last pass can overshoot.
Count the steps
How many times does the body run?
int n = 6, steps = 0;
while (n != 1) {
n = (n % 2 == 0) ? n / 2 : 3 * n + 1;
steps++;
}
System.out.println(steps);68916
Show the answer
n goes 6, 3, 10, 5, 16, 8, 4, 2, 1: that's 8 updates. The loop exits as soon as the check sees n == 1.
The update that escaped
The i++ below sits outside the loop's braces. Inside, i stays 0 forever, i < 5 is always true, and the program prints 0 until you kill it. Forgetting (or misplacing) the update is a classic cause of infinite while loops.
int i = 0;
while (i < 5) {
System.out.println(i);
}
i++; // never reachedSafer stop conditions
int x = 1;
while (x != 10) {
x += 2;
}x is always odd (1, 3, ... 9, 11...), so it skips 10 and never stops. Even after int overflow wraps around, it stays odd.
int x = 1;
while (x < 10) {
x += 2;
}< stops as soon as x passes 10, whatever the step size.
In real projects
A while loop whose exit is never reached shows up in production as a server stuck at 100% CPU, a frozen app, or a request that never returns. Defensive habits: prefer range checks (<) over exact ones (!=), and for loops that wait on the outside world, add a maximum number of attempts or a timeout.
Key takeaways
- The condition is checked BEFORE each pass
- It may run zero times
- Forgetting to update the loop variable gives an infinite loop
💡 Like checking the fridge before each snack: if it's empty from the start, you never eat at all.
The "count the steps" loop is the Collatz problem from 1937. Nobody has ever proved it reaches 1 for every starting number, although computers have checked enormous ranges and it always has.
Practice questions
What does this print?
int count = 10;
while (count < 5) {
System.out.println(count);
count++;
}
System.out.println("end");- 10 end
- end
- 5 end
Check your answer
end. 10 < 5 is false on the very first check, so the body never runs and only end prints.
What does this print?
int i = 1;
while (i < 20) {
i *= 3;
}
System.out.println(i);- 9
- 20
- 27
- 81
Check your answer
27. i goes 1, 3, 9, 27. When i is 9, 9 < 20 is still true, so the body runs once more and makes it 27. Then 27 < 20 fails.