TOPIC 2.7 · STUDY
while Loops
A while loop repeats its body as long as a Boolean condition stays true. The condition is checked before every pass, including the first. Use while when how many times you repeat depends on a value that changes inside the loop.
2.7.A
When a while loop is the right structure
A while loop keeps running its body while a condition evaluates to true. Each time the body finishes, the program returns to the condition and asks again. That makes while the right tool when you know what must stay true to continue, but you do not know ahead of time how many passes will happen.
An if statement is different. It checks once and runs its body at most one time. If the same work must happen again and again as a value changes, copying the same lines by hand is brittle and easy to get wrong. A while loop names the repeating work once and lets the condition control how long it lasts.
int fuel = 12;
while (fuel >= 3) {
System.out.print("burn");
fuel -= 3;
}
System.out.print("dock");
// prints burnburnburnburndock
// fuel ends at 0
fuel starts at 12. While at least 3 units remain, the body prints burn and subtracts 3. After four burns, fuel is 0, the condition fails, and dock runs once after the loop. The number of burns came from the changing fuel value, not from writing four separate burn statements.
True sends control into the body, then back up to the condition. False leaves the loop and continues with the next statement.
Key points
- while repeats its body while the condition stays true.
- Use while when the repeat count depends on a changing value.
- An if check runs its body at most once.
- Do not unroll the same task into copied statements.
- Code after the loop runs only when the condition becomes false.
Choose while when you can state the keep-going rule clearly, such as fuel >= 3, even if you cannot predict the number of passes before the program runs.
2.7.B
How the condition is tested
Java tests the while condition before every body execution, including the first. If the condition is already false when the loop is reached, the body is skipped entirely and control moves to the statements after the closing brace.
int crates = 0;
while (crates > 0) {
crates--;
System.out.print("load");
}
System.out.print("idle");
// prints idle
// body never runs
crates starts at 0, so crates > 0 is false on the first check. load never prints, and idle runs right away. That is normal while behavior, not an error: zero passes is allowed when the keep-going rule fails at the start.
The body must change at least one value that the condition depends on. If nothing in the body moves those values toward false, the condition stays true and the loop never ends. Off-by-one mistakes usually come from the wrong comparison, such as > instead of >=, or from updating the control value too early or too late relative to the work in the body.
int ping = 3;
while (ping > 0) {
System.out.print(ping);
ping--;
}
// prints 321
int early = 3;
while (early > 0) {
early--;
System.out.print(early);
}
// prints 210
int stuck = 2;
while (stuck > 0) {
System.out.print("tick");
}
// infinite: stuck never changes
The first loop prints the value, then decreases it, so the output is 321. The second decreases first, so the printed values are 210. Same starting number and same comparison, different update timing, different result. The third loop prints tick forever because stuck is never updated.
Key points
- The condition is tested before each body run, including the first.
- A false start means the body never runs; later code still runs.
- Update the values the condition uses, or the loop is infinite.
- Wrong comparisons cause off-by-one iteration counts.
- Updating too early or too late changes what each pass sees.
A body that does not change the condition can run forever. Always make sure some statement inside the loop moves a watched value toward making the condition false.