📖 Scenario: You are organizing a small event and want to print the timeslots available every 15 minutes from 9:00 AM to 10:00 AM.
🎯 Goal: Build a Swift program that uses stride(from:to:by:) to generate timeslots with a custom step of 15 minutes and print them.
📋 What You'll Learn
Create a variable called startTime with the value 9.0 representing 9:00 AM
Create a variable called endTime with the value 10.0 representing 10:00 AM
Create a variable called step with the value 0.25 representing 15 minutes (quarter of an hour)
Use stride(from:to:by:) with startTime, endTime, and step to loop through the timeslots
Print each timeslot in the format "Time slot: X" where X is the current time
💡 Why This Matters
🌍 Real World
Scheduling events or appointments often requires generating timeslots with specific intervals.
💼 Career
Understanding how to use stride helps in tasks like calendar apps, timers, and any time-based data processing.
Progress0 / 4 steps
1
Create start and end time variables
Create a variable called startTime and set it to 9.0. Also create a variable called endTime and set it to 10.0.
Swift
Hint
Use let to create constants for startTime and endTime.
2
Create step variable for 15 minutes
Create a variable called step and set it to 0.25 to represent 15 minutes as a fraction of an hour.
Swift
Hint
Remember 15 minutes is one quarter of an hour, so use 0.25.
3
Use stride to loop through timeslots
Use stride(from:to:by:) with startTime, endTime, and step to loop through the timeslots. Use a for loop with variable time.
Swift
Hint
Use for time in stride(from: startTime, to: endTime, by: step) to loop.
4
Print each timeslot
Inside the for loop, print the current time using print("Time slot: \(time)").
Swift
Hint
Use print("Time slot: \(time)") inside the loop to show each time.
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
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.
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.
Final Answer:
Generates numbers starting at 1, increasing by 2, up to but not including 10. -> Option C
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
Step 1: Identify counting down with inclusion
Counting down from 10 to 2 means the step is negative (-2). Including 2 means using through.
Step 2: Match syntax to requirements
Only stride(from: 10, through: 2, by: -2) matches counting down with inclusion of 2.
Final Answer:
stride(from: 10, through: 2, by: -2) -> Option A
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
Step 1: Analyze the stride parameters
The loop starts at 0, goes up to but excludes 5, stepping by 2.
Step 2: List the values printed
Values are 0, 2, 4. It stops before reaching 5.
Final Answer:
0 2 4 -> Option A
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
Step 1: Check the step parameter
The step is zero, which is invalid because it causes no progress in the loop.
Step 2: Understand the effect of zero step
A zero step causes a runtime error or infinite loop because the loop never advances.
Final Answer:
Step value cannot be zero; it causes a runtime error. -> Option B
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
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.
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.
Final Answer:
for i in stride(from: 1, through: 15, by: 2) { if i > 5 { print(i) } } -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.