The third part of the header
In for (initialization; condition; iteration), the iteration part runs after every pass and decides how far the counter moves. So far that was i++, a step of 1. Replacing it with i += 2 makes the counter skip every other number.
Even numbers from 2 to 10
for (int i = 2; i <= 10; i += 2) {
Console.WriteLine(i);
}
The output is:
2
4
6
8
10
- The loop starts at 2, an even number.
i <= 10includes 10. Writingi < 11would give the same result.i += 2adds 2 after each pass, so the counter stays even.
The key idea is that adding 2 never changes whether a number is even or odd. The first value therefore decides which kind of number the loop prints.
The exercise: odd numbers from 7 to 23
The task is to print the odd numbers 7, 9, 11 and so on up to 23. It has two steps.
- Write the header. Start at 7, the first odd number in the range, so the loop prints odd numbers only. Stop with
i <= 23, because 23 is odd and should be printed. Step withi += 2. - Print the counter with
Console.WriteLine(i).
for (int i = 7; i <= 23; i += 2) {
Console.WriteLine(i);
}
The counter takes the values 7, 9, 11, 13, 15, 17, 19, 21 and 23. After 23 it becomes 25, which fails i <= 23, and the loop ends.
Other step sizes
The step can be any number. i += 5 counts in fives, and i += 10 counts in tens. A step larger than 1 means the counter may never equal the upper bound exactly, as when starting at 7 and stepping by 3 towards 23. That is why loops use <= or < in the condition rather than !=: a range check works whether or not the counter lands on the limit.
Common mistakes
- Starting on the wrong parity. Starting at 6 with
i += 2prints even numbers, however the condition is written. - Using
i + 2instead ofi += 2. The expressioni + 2computes a value and throws it away, so the counter never changes and the loop never ends. Usei += 2ori = i + 2. - Using
!=as the stop condition.i != 24with odd numbers never matches, and the loop keeps going past 23. - Stopping one step early.
i < 23leaves out 23 itself.
A trace of the exercise loop
Reading the loop pass by pass shows where the iteration part acts:
| Pass | Value of i | Condition i <= 23 | Printed |
|---|---|---|---|
| 1 | 7 | true | 7 |
| 2 | 9 | true | 9 |
| 9 | 23 | true | 23 |
| end | 25 | false | nothing |
Another example is for (int i = 0; i <= 12; i += 3), which prints 0, 3, 6, 9 and 12.
Stepping versus testing
There is a second way to print odd numbers: loop over every number and test each one.
for (int i = 7; i <= 23; i++) {
if (i % 2 != 0) {
Console.WriteLine(i);
}
}
This prints the same output but makes 17 passes instead of 9. Stepping by 2 is shorter and does less work. The test is still useful when the start value is not known in advance. It is written as i % 2 != 0 rather than i % 2 == 1 because the remainder of a negative odd number is negative in C#: -3 % 2 is -1.
Why the starting value matters
With a step of 2, the loop only ever sees numbers of the same parity as its start. That is why the exercise starts at 7 and not at 8: the loop cannot move from an even number to an odd one by adding 2. If a task says "odd numbers from 8 to 23", the first odd number is 9, so that is where the loop should start.
Key takeaways
- The iteration part sets the step size:
i += 2skips every other number. - The starting value decides whether the loop prints odd or even numbers.
- Use a range condition such as
<=rather than!=when the step is more than 1.