Bird
Raised Fist0
Swiftprogramming~10 mins

Stride for custom step in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Stride for custom step
Start at 'from'
↓
Check: current < to?
No→EXIT
Yes↓
Execute loop body
↓
Add 'by' to current
↩Back to Check
This flow shows how stride starts from a value, checks if it is less than the end, runs the loop body, then adds the step size before repeating.
Execution Sample
Swift
for i in stride(from: 0, to: 10, by: 3) {
    print(i)
}
This code prints numbers from 0 up to (but not including) 10, stepping by 3 each time.
Execution Table
Iterationi (current value)Condition (i < 10)ActionOutput
10trueprint(i), i = i + 30
23trueprint(i), i = i + 33
36trueprint(i), i = i + 36
49trueprint(i), i = i + 39
512falseexit loop
💡 i reaches 12, condition 12 < 10 is false, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
i03691212
Key Moments - 3 Insights
Why does the loop stop before printing 12?
Because the condition i < 10 becomes false when i is 12, so the loop exits before printing 12, as shown in execution_table row 5.
What does the 'by' parameter control?
The 'by' parameter controls how much i increases each time, here it adds 3 after each print, as seen in variable_tracker.
Why is the last printed value 9 and not 10?
Because stride uses 'to' which excludes the end value 10, so it stops before reaching 10, shown in the condition checks in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i at iteration 3?
A6
B9
C3
D12
💡 Hint
Check the 'i (current value)' column at iteration 3 in the execution_table.
At which iteration does the condition i < 10 become false?
A4
B5
C3
D2
💡 Hint
Look at the 'Condition (i < 10)' column in execution_table where it becomes false.
If the 'by' value changed to 4, what would be the output at iteration 2?
A6
B3
C4
D8
💡 Hint
With 'by' = 4, i increases by 4 each iteration starting from 0, so after first iteration i is 4.
Concept Snapshot
Stride syntax: stride(from: start, to: end, by: step)
Loops from start up to but not including end
Increments by step each time
Stops when current value >= end
Useful for custom step loops
Full Transcript
This visual execution shows how Swift's stride function works with a custom step. Starting at 0, it checks if the current value is less than 10. If yes, it prints the value, then adds 3. This repeats until the value reaches 12, which fails the condition and stops the loop. The variable i changes from 0 to 3, 6, 9, then 12. The loop prints 0, 3, 6, and 9. The step size controls how much i increases each time. The loop excludes the end value 10 because 'to' is used. This helps understand how stride controls loop steps and limits.

Practice

(1/5)
1. What does the Swift function stride(from: 1, to: 10, by: 2) do?
easy
A. Generates numbers starting at 1, decreasing by 2, up to but not including 10.
B. Generates numbers starting at 1, increasing by 2, including 10.
C. Generates numbers starting at 1, increasing by 2, up to but not including 10.
D. Generates numbers starting at 1, decreasing by 2, including 10.

Solution

  1. Step 1: Understand the parameters of stride

    The function uses from: 1 as the start, to: 10 as the end (exclusive), and by: 2 as the step size.
  2. Step 2: Determine the sequence generated

    It generates numbers starting at 1, adding 2 each time, stopping before reaching 10, so the sequence is 1, 3, 5, 7, 9.
  3. Final Answer:

    Generates numbers starting at 1, increasing by 2, up to but not including 10. -> Option C
  4. Quick Check:

    stride(from:1, to:10, by:2) excludes 10 [OK]
Hint: Remember: 'to' excludes the end value in stride [OK]
Common Mistakes:
  • Confusing 'to' with 'through' and including the end value
  • Mixing up positive and negative step directions
  • Assuming the step is always 1
2. Which of the following is the correct syntax to create a stride in Swift that counts down from 10 to 2 by steps of 2, including 2?
easy
A. stride(from: 10, through: 2, by: -2)
B. stride(from: 10, to: 2, by: -2)
C. stride(from: 2, through: 10, by: 2)
D. stride(from: 2, to: 10, by: 2)

Solution

  1. Step 1: Identify counting down with inclusion

    Counting down from 10 to 2 means the step is negative (-2). Including 2 means using through.
  2. Step 2: Match syntax to requirements

    Only stride(from: 10, through: 2, by: -2) matches counting down with inclusion of 2.
  3. Final Answer:

    stride(from: 10, through: 2, by: -2) -> Option A
  4. Quick Check:

    Use 'through' to include end, negative step for countdown [OK]
Hint: Use 'through' to include end; negative step for countdown [OK]
Common Mistakes:
  • Using 'to' instead of 'through' when including the end
  • Using positive step when counting down
  • Swapping start and end values
3. What is the output of the following Swift code?
for i in stride(from: 0, to: 5, by: 2) {
    print(i)
}
medium
A. 0 2 4
B. 0 2 4 6
C. 0 2 4 5
D. 2 4

Solution

  1. Step 1: Analyze the stride parameters

    The loop starts at 0, goes up to but excludes 5, stepping by 2.
  2. Step 2: List the values printed

    Values are 0, 2, 4. It stops before reaching 5.
  3. Final Answer:

    0 2 4 -> Option A
  4. Quick Check:

    Stride 'to' excludes end, so 5 is not printed [OK]
Hint: Remember 'to' excludes end value in stride loops [OK]
Common Mistakes:
  • Including the end value 5 mistakenly
  • Adding an extra step beyond the end
  • Confusing step size with end value
4. Identify the error in this Swift code snippet:
for i in stride(from: 1, through: 10, by: 0) {
    print(i)
}
medium
A. Using 'through' instead of 'to' is invalid syntax.
B. Step value cannot be zero; it causes a runtime error.
C. Start value must be less than end value for positive step.
D. The loop will run infinitely without errors.

Solution

  1. Step 1: Check the step parameter

    The step is zero, which is invalid because it causes no progress in the loop.
  2. Step 2: Understand the effect of zero step

    A zero step causes a runtime error or infinite loop because the loop never advances.
  3. Final Answer:

    Step value cannot be zero; it causes a runtime error. -> Option B
  4. Quick Check:

    Step must not be zero in stride [OK]
Hint: Step cannot be zero; always non-zero positive or negative [OK]
Common Mistakes:
  • Thinking 'through' is invalid syntax
  • Assuming zero step means step of 1
  • Ignoring runtime errors caused by zero step
5. You want to create a Swift loop that prints all odd numbers from 1 to 15 inclusive, but only those greater than 5. Which stride and condition combination achieves this?
hard
A. for i in stride(from: 1, to: 15, by: 2) { if i >= 5 { print(i) } }
B. for i in stride(from: 5, to: 15, by: 2) { print(i) }
C. for i in stride(from: 1, through: 15, by: 2) { if i >= 5 { print(i) } }
D. for i in stride(from: 1, through: 15, by: 2) { if i > 5 { print(i) } }

Solution

  1. Step 1: Identify stride to generate odd numbers 1 to 15 inclusive

    Using stride(from: 1, through: 15, by: 2) generates 1,3,5,...,15 including 15.
  2. Step 2: Filter numbers greater than 5

    The condition if i > 5 prints only numbers greater than 5, i.e., 7,9,11,13,15.
  3. Final Answer:

    for i in stride(from: 1, through: 15, by: 2) { if i > 5 { print(i) } } -> Option D
  4. Quick Check:

    Stride with 'through' includes 15; condition filters >5 [OK]
Hint: Use 'through' to include end; filter inside loop for conditions [OK]
Common Mistakes:
  • Using 'to' excludes 15, missing last number
  • Incorrect condition filtering numbers equal to 5
  • Starting stride at 5 misses odd numbers less than 5
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard