Why the iteration part matters
The third part of a for header, the iteration, runs after every pass of the body. Most of the time it is i++, which moves one number at a time. Replace it with something else and the loop skips numbers. The lesson's exercise uses a step of 2 to print every odd number from 7 through 23.
Stepping by 2
This loop prints the even numbers from 2 through 10:
for (let i = 2; i <= 10; i += 2) {
console.log(i);
}
2
4
6
8
10
let i = 2starts at the first even number.i <= 10keeps the loop going whileiis at most 10. Writingi < 11would give the same result.i += 2adds 2 after each pass. It is shorthand fori = i + 2.
Any assignment can be the iteration. i += 5 counts in fives, i -= 1 counts down, and i *= 2 doubles the value every time, giving 1, 2, 4, 8 and so on.
Odd or even depends on the start
Adding 2 never changes whether a number is odd or even. A loop that starts at an even number and steps by 2 visits only even numbers, and a loop that starts at an odd number visits only odd numbers. So to print odd numbers, there is no need for a check with % inside the loop. It is enough to start at an odd number and step by 2.
The exercise
The task is to print the odd numbers from 7 through 23: 7, 9, 11, and so on up to 23.
- Choose the start. The first number is 7, which is odd, so
let i: number = 7. - Choose the condition. 23 must be printed, so the loop continues while
i <= 23. - Choose the step.
i += 2moves from one odd number to the next. - Print the value in the body with
console.log(i).
The solution in TypeScript
for (let i: number = 7; i <= 23; i += 2) {
console.log(i);
}
The output is 7, 9, 11, 13, 15, 17, 19, 21 and 23, nine lines in all. After the pass for 23, i becomes 25, the condition 25 <= 23 is false, and the loop ends.
Checking the count
With a step of 2, the number of passes is (last - first) / 2 + 1 when the last value is actually reached. Here that is (23 - 7) / 2 + 1 = 9, which matches the nine lines above.
When the step skips over the limit
If the last value is not on the path, a condition with <= or < still ends the loop correctly. Starting at 7 and stepping by 2 up to a limit of 24 prints the same nine numbers, because 25 is above 24. This is the reason to use a range comparison here rather than testing for an exact value such as i !== 24, which the counter would never hit and which would make the loop run forever.
Common mistakes
- Starting at the wrong parity. Starting at 8 prints 8, 10, 12 and so on, which are even numbers. Check the first line of the output.
- Using
i++. That prints every number from 7 to 23, even and odd, instead of every second one. - Using
i + 2without assigning. The expressioni + 2computes a value and throws it away. The counter never changes, and the loop never ends. The update has to be an assignment such asi += 2. - An off-by-one limit. With
i < 23, 23 is not printed. The task says "through 23", so use<=. - Stepping by 2 with a start outside the range. A start of 5 prints 5 as well, which the task does not want.
Key takeaways
- The iteration part can be any update:
i += 2,i -= 3,i *= 2. - A step of 2 keeps the parity of the start value, so starting at an odd number visits only odd numbers.
- Use a range condition (
<=,<) so the loop still stops if the step jumps over the limit. - The update must change the loop variable, or the loop runs forever.