Start learning free

LESSON 121 OF 348 · WHILE LOOPS & FOR LOOPS

for Loops in TypeScript: Changing the Iteration Step

Lesson Read the full explanation

Let's see an example where we change the iteration statement.

We can print all even numbers from 2 through 10:

for (let i = 2; i <= 10; i += 2) {
	console.log(i);
}

The output of this code is:

2
4
6
8
10

Let's break down this code:

  • The initialization statement is let i = 2, so we start iterating from number 2.

  • The condition statement is i <= 10 so we end at number 10. We could've written i < 11 and it would still be correct.

  • The iteration statement is i += 2, so we increase our number by 2 every time.


Assignment

Let's print all odd numbers from 7 through 23 using a for loop.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    TYPESCRIPT · WHILE LOOPS & FOR LOOPS

    Printing Numbers 4 in TypeScript, explained

    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 = 2 starts at the first even number.
    • i <= 10 keeps the loop going while i is at most 10. Writing i < 11 would give the same result.
    • i += 2 adds 2 after each pass. It is shorthand for i = 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.

    1. Choose the start. The first number is 7, which is odd, so let i: number = 7.
    2. Choose the condition. 23 must be printed, so the loop continues while i <= 23.
    3. Choose the step. i += 2 moves from one odd number to the next.
    4. 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 + 2 without assigning. The expression i + 2 computes a value and throws it away. The counter never changes, and the loop never ends. The update has to be an assignment such as i += 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.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free