2.7 while loopsRepeat while it's true

A while loop repeats its body as long as its condition is true. Java checks the condition before every pass, including the very first one. If it's true, the body runs and Java goes back to check again. If it's false, the loop ends and the program continues after the closing brace. If the condition is false from the start, the body runs zero times.
Something in the body must eventually make the condition false. If nothing does, you get an infinite loop: it never stops. while (n > 0) { sum += n; } with no change to n runs forever. The other classic mistake is an off-by-one error, where the loop runs one pass too many or one too few, usually because of < versus <=.
A return statement inside a loop ends the whole method at once, loop and all. That's useful when you're searching for something: as soon as you find it, return.
?Wait, how did n % 10 and n / 10 work on digits?
1int n = 5308;2int sum = 0;3int passes = 0;4while (n > 0)15{6 sum += n % 10;27 n /= 10;38 passes++;9}10System.out.println(sum + " " + passes);
- line 4 Checked before every pass, including the first
- line 6 n % 10 is the last digit
- line 7 Drops the last digit: this moves n toward 0
| Pass | n % 10 | sum | n after | n > 0 next? |
|---|---|---|---|---|
| start | 0 | 5308 | true | |
| 1 | 8 | 8 | 530 | true |
| 2 | 0 | 8 | 53 | true |
| 3 | 3 | 11 | 5 | true |
| 4 | 5 | 16 | 0 | false: stop |
while loops shine when you don't know in advance how many passes you'll need. How many years until a savings account doubles at 7 percent a year? Start with double money = 1000.0; int years = 0; and loop while (money < 2000) { money *= 1.07; years++; }. It stops after 11 years, when money first reaches 2000 or more (about 2104.85).
?Wait, what does money *= 1.07 do?
I thought about it. Show me
What is printed?
int n = 6;
int steps = 0;
while (n != 1)
{
if (n % 2 == 0)
{
n /= 2;
}
else
{
n = 3 * n + 1;
}
steps++;
}
System.out.println(steps);- Set up a trace table with columns for n and steps. Start: n is 6, steps is 0. 6 != 1, so enter the loop.
Why?
Check the condition before every pass, including the first. - Pass 1: 6 is even, so n becomes 3. steps is 1. Pass 2: 3 is odd, so n becomes 10. steps is 2.
- Pass 3: 10 becomes 5 (steps 3). Pass 4: 5 is odd, so 16 (steps 4). Pass 5: 8 (steps 5). Pass 6: 4 (steps 6). Pass 7: 2 (steps 7). Pass 8: 1 (steps 8).
- Check again: n is 1, so 1 != 1 is false. The loop ends, and 8 is printed.
Why?
Don't stop the trace one pass early. The last pass is the one that makes the condition false.
Answer: 8 (n goes 6, 3, 10, 5, 16, 8, 4, 2, 1)
Stuck? Get a hint
- Make a table with n and steps. Write a new row after every pass.
- Even: halve it. Odd: triple it and add 1. Then add 1 to steps.
- Stop only when the check finds n equal to 1.
- Aint a = 1; while (a < 100) { a *= 2; }
- Bint b = 10; while (b > 0) { b -= 3; }
- Cint c = 0; while (c != 7) { c += 2; }
- Dint d = 50; while (d > 1) { d /= 2; }
Show the answer
C. c goes 0, 2, 4, 6, 8, 10, and so on. It jumps right over 7, so c != 7 is always true and the loop never stops. The others all move toward making their conditions false: a doubles past 100, b drops below 1, and d halves down to 1. Choice B is the usual suspect, but b reaches -2 and stops.
- A while loop checks its condition before every pass. False at the start means zero passes.
- The body must move the loop toward stopping, or it's an infinite loop.
- Trace with a table: one row per pass, and check the condition after every update.
- return inside a loop ends the method immediately.