Changing the step
The update statement doesn't have to add 1. Any change to the counter works, as long as it moves toward the point where the condition becomes false. This lesson shows two variations: a bigger step, and counting down.
Even numbers from 4 through 14
int i = 4;
while (i <= 14) {
Console.WriteLine(i);
i += 2;
}
4
6
8
10
12
14
The counter starts at the first even number, and i += 2 jumps to the next one. Because the start is even and the step is 2, every value stays even. The loop prints 14 and then moves to 16, which makes i <= 14 false.
Even numbers from 12 down to 4
To count down, start at the larger number, subtract in the update, and flip the comparison so that it checks the lower limit:
int i = 12;
while (i >= 4) {
Console.WriteLine(i);
i -= 2;
}
12
10
8
6
4
i -= 2 is shorthand for i = i - 2. In the same way, i += 2 is shorthand for i = i + 2.
A recipe for any range
Three questions give you the whole loop:
- Where does it start? This is the initial value of the counter.
- Which direction, and where does it end? Counting up uses
<=with the last value. Counting down uses>=with the last value. - How big is each step? This is the update, such as
+= 2or-= 2.
Using the wrong direction is a common mistake. If you count down but the condition says i <= 4 with a start of 12, the condition is false on the first check and nothing prints. If you count up with a condition that is always true, the loop never ends.
When the step doesn't land on the limit
The limit in the condition is a boundary, not a target. If the step skips over it, the loop simply stops at the last value that passes the check. Starting at 4 with j += 3 and the condition j <= 14 prints these numbers:
4 7 10 13
The next value would be 16, and 14 never appears. This is fine, and often what you want, but it's a reminder to work out the first and last values by hand before running the loop.
The exercise, step by step
The assignment is to print the odd numbers from 23 down to 7, in decreasing order.
- Start at 23.
int counter = 23;is the largest number to print, and it's odd. - Count down to 7. Use
while (counter >= 7). The comparison is>=because the counter decreases and 7 must be included. - Print the counter.
Console.WriteLine(counter);prints each value on its own line. - Subtract 2.
counter -= 2;moves from one odd number to the previous one. Subtracting 2 from an odd number always gives an odd number.
int counter = 23;
while (counter >= 7) {
Console.WriteLine(counter);
counter -= 2;
}
The output is 23, 21, 19, 17, 15, 13, 11, 9, 7, which is nine lines. After 7 is printed, counter becomes 5 and the condition 5 >= 7 is false, so the loop ends.
Common mistakes
- Starting on an even number. Starting at 22 or 24 gives even values for the whole loop. The first value decides the parity when the step is 2.
- Using
+=when counting down. The counter would grow away from the limit, socounter >= 7is always true and the loop never ends. - Using the wrong comparison.
counter <= 7is false from the start, because 23 is not at most 7. Nothing is printed. - Stopping too early.
counter > 7leaves out 7.
Key takeaways
- The update can be any step:
+= 2counts up by twos, and-= 2counts down by twos. - Counting up uses
<or<=. Counting down uses>or>=. - The starting value and the step decide whether you get odd or even numbers.
- The update must always move the counter toward the exit condition.