Changing the step
A counter doesn't have to go up by 1. The update line decides how far the variable moves on each pass, so changing it changes which numbers are visited. This loop prints the even numbers from 4 through 14:
var i = 4
while (i <= 14) {
println(i)
i += 2
}
It prints 4, 6, 8, 10, 12 and 14, one per line. The counter starts at an even number and goes up by 2, so it stays even. After 14, i becomes 16, the check 16 <= 14 is false, and the loop ends.
Counting down
A loop can also run in reverse. Start high, keep going while the counter is at least the lowest value, and subtract on each pass:
var i = 12
while (i >= 4) {
println(i)
i -= 2
}
This prints 12, 10, 8, 6 and 4. Two things changed compared with counting up: the comparison is now >=, and the update is -=. If you subtract but keep <=, or add but use >=, the counter moves away from the stopping point. The condition is then either false from the start, and the loop never runs, or true forever, and the loop never ends.
The exercise
The task is to print the odd numbers from 23 down to 7, in decreasing order. The numbers are 23, 21, 19 and so on down to 7. It has four steps.
- Start at the first number. The sequence begins at 23, so declare
var counter = 23. Starting on an odd number matters: stepping by 2 keeps the counter odd. - Choose the condition. The last number to print is 7, so the loop must keep going while
counter >= 7. Since we are counting down, the comparison points the other way from the counting-up examples. - Print the counter with
println(counter)inside the body. - Move to the previous odd number. Odd numbers are 2 apart, so subtract 2 with
counter -= 2.
var counter = 23
while (counter >= 7) {
println(counter)
counter -= 2
}
The output is 23, 21, 19, 17, 15, 13, 11, 9 and 7. After printing 7, the counter becomes 5, the check 5 >= 7 is false, and the loop stops. The body runs nine times.
Checking your work
A quick way to test a loop is to check its first and last printed values by hand. Here the first value is the starting value, 23. The last printed value is the last one that passes the test: 7 passes 7 >= 7, and the next value, 5, fails it. If your loop prints 5 or stops at 9, the condition or the step is off.
Gotchas
- Starting on the wrong parity. Starting at 22 and subtracting 2 prints even numbers, ending at 8. The starting value decides whether you get odd or even numbers.
- Wrong comparison direction. With
counter <= 7the condition is false at 23, so nothing is printed. - Wrong sign in the update. With
counter += 2the counter climbs away from 7. The conditioncounter >= 7never becomes false, so the loop runs forever. - Stepping over the end. The end value must be reachable from the start with the chosen step. If the loop used
counter > 7, 7 would be skipped. - Printing before or after the update. The body prints first and updates second. Swap the two lines and the first number printed is 21, not 23.
Key takeaways
- The update statement sets the step:
+= 2skips every other number and-= 2counts down by twos. - Counting up uses
<or<=with+, and counting down uses>or>=with-. - The starting value decides which numbers appear, for example odd or even.
- The condition and the update must push in the same direction, or the loop never ends.