Changing the step and the direction
The counting loop from the previous lesson adds 1 on every pass. The update does not have to be that. Change the update and you change which numbers appear. Change the comparison and the start and you can count downward. The structure stays the same: a variable, a condition and an update.
Example 1: even numbers going up
To print the even numbers from 4 through 14, start at 4 and add 2 each time:
i := 4
for i <= 14 {
fmt.Println(i)
i += 2
}
It prints 4, 6, 8, 10, 12 and 14, one per line. Because 4 is even and every step adds an even number, i stays even. The last value, 14, passes the check 14 <= 14. Then i becomes 16, the check fails and the loop ends.
Example 2: even numbers going down
To count in reverse, start at the large end, subtract, and flip the comparison:
i := 12
for i >= 4 {
fmt.Println(i)
i -= 2
}
This prints 12, 10, 8, 6 and 4. The condition is now i >= 4 because the loop must keep going while the value is still at or above the lower limit. The loop ends when i falls to 2.
Compare the two examples. When counting up, the condition has an upper limit (<= or <) and the update adds. When counting down, the condition has a lower limit (>= or >) and the update subtracts. If the direction of the update and the direction of the comparison disagree, the loop either never runs or never ends.
The exercise
Print all the odd numbers from 23 down to 7, largest first. Decide the three parts first:
- Start: the largest number is 23, so declare
counterwith:=and the value 23. - Condition: the smallest number to print is 7, and the loop counts downward, so it runs while
counter >= 7. - Body: print
counterwithfmt.Println. - Update: subtract 2. The distance between two neighbouring odd numbers is 2, and subtracting 2 from an odd number gives an odd number.
counter := 23
for counter >= 7 {
fmt.Println(counter)
counter -= 2
}
The output is 23, 21, 19, 17, 15, 13, 11, 9 and 7, which is nine numbers. After 7 is printed, counter becomes 5, the check 5 >= 7 fails and the loop ends.
Checking the boundaries
Before you run a counting loop, test its two ends by hand. For the exercise, the first pass checks 23 >= 7, which is true, so 23 is printed. The last wanted value is 7. Does it pass the check? 7 >= 7 is true, so yes. What comes right after? 5, and 5 >= 7 is false, so nothing below 7 is printed. These three checks, first value, last value and first value past the end, catch most off-by-one mistakes.
The same habit works for Example 1. The last wanted value is 14, and 14 <= 14 is true. The next value is 16, which fails the check. If the task had asked for the numbers below 14, the condition would be i < 14 instead.
Why the start value matters
The loop prints only odd numbers because it starts on an odd number and moves in steps of 2. If you started from 24, you would get even numbers, and the loop would stop at 8, since 6 is below the limit. The step does not filter anything. It carries the starting parity along.
Common mistakes
- Counting down with
<=:for counter <= 7is false at 23, so nothing is printed. - Subtracting without changing the comparison, or adding while counting down. With
counter += 2andcounter >= 7the value grows and the condition stays true, so the loop never ends. - Using
> 7when 7 must be printed. That stops at 9. - Forgetting that the variable keeps changing after the loop: here
counteris 5 when the loop finishes.
Key takeaways
- The update decides the step size, and the starting value decides which numbers the step reaches.
- To count down, start high, use
>=or>, and subtract. - Always check that the update moves the variable toward the end of the condition.