Start learning free

LESSON 126 OF 415 · WHILE LOOPS & FOR LOOPS

Printing Numbers II in Swift: stride(from:through:by:)

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 i in stride(from: 2, through: 10, by: 2) {
	print(i)
}

The output of this code is:

2
4
6
8
10

Let's break down this code:

  • The initialization statement is from: 2, so we start iterating from number 2.

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

  • The iteration statement is by: 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

    SWIFT · WHILE LOOPS & FOR LOOPS

    Printing Numbers 4 in Swift, explained

    When a range is not enough

    A range such as 2...10 always moves by 1. To count in bigger steps, for example every second number, Swift offers the stride function. It produces a sequence of numbers with a start, an end and a step, and a for-in loop can go through it:

    for i in stride(from: 2, through: 10, by: 2) {
        print(i)
    }
    

    The output is:

    2
    4
    6
    8
    10
    

    The three parts of stride

    • from: 2 is the first value of the sequence, so the loop starts at 2.
    • through: 10 is the last value allowed. If the steps land exactly on 10, it is included.
    • by: 2 is the step added to the value each time.

    The loop variable i takes the values 2, 4, 6, 8 and 10 in turn, the same way it would for a range.

    through versus to

    There are two versions of stride, and they differ in how they treat the end value:

    • stride(from:through:by:) includes the end value when a step lands on it.
    • stride(from:to:by:) never includes the end value, like the half-open range ..<.

    With the end value 10 and a step of 2, the two behave differently:

    for i in stride(from: 2, to: 10, by: 2) {
        print(i, terminator: " ")
    }
    
    2 4 6 8 
    

    This stops at 8 because 10 is excluded. Writing to: 11 brings 10 back, since 11 is not reached and 10 is below it. In both versions, a step that doesn't land on the end value simply stops at the last value before it. For example, stride(from: 2, through: 9, by: 2) gives 2, 4, 6 and 8.

    The exercise: odd numbers from 7 through 23

    The task is to print the odd numbers between 7 and 23, with both ends included. It is built in two steps.

    1. Write the loop header. The first odd number is 7, and the odd numbers are 2 apart, so the start is from: 7 and the step is by: 2. The last number to print is 23, and the task says "through", so use through: 23.
    2. Print i in the body. Each pass prints the current value on its own line.
    for i in stride(from: 7, through: 23, by: 2) {
        print(i)
    }
    

    The values are 7, 9, 11, 13, 15, 17, 19, 21 and 23. Starting on an odd number and adding 2 each time keeps every value odd, so no check with % is needed.

    Common mistakes

    • Using to: when the end must be included. stride(from: 7, to: 23, by: 2) stops at 21.
    • Starting on an even number. From 8 with a step of 2 every value is even, so the odd numbers never appear.
    • Mixing up the labels. The labels from:, through: (or to:) and by: are required and must come in this order.
    • Using a step of 0. A step of zero would never make progress, and Swift stops the program with a run-time error.
    • Using a negative step to count up. With from: 2, through: 10, by: -2 the sequence is empty, because the step moves away from the end value. Counting down is covered in the next lesson.

    Key takeaways

    • stride lets a for-in loop move in steps other than 1.
    • through: includes the end value and to: excludes it.
    • Starting on an odd number and stepping by 2 visits only odd numbers.
    • The step in by: must not be 0.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free