Bird
0
0

Why does stride(from: 1, to: 10, by: 3) exclude 10, but stride(from: 1, through: 10, by: 3) might include it?

hard📝 Conceptual Q10 of 15
Swift - Loops
Why does stride(from: 1, to: 10, by: 3) exclude 10, but stride(from: 1, through: 10, by: 3) might include it?
ABoth include the end value always
B'to' includes the end value, 'through' excludes it
CBoth exclude the end value always
D'to' excludes the end value, 'through' includes it if step lands exactly
Step-by-Step Solution
Solution:
  1. Step 1: Understand 'to' vs 'through'

    'to' creates a sequence up to but not including the end value. 'through' includes the end value if the step lands exactly on it.
  2. Step 2: Apply to example

    With step 3 from 1: values are 1,4,7,10. Using 'to' excludes 10, using 'through' includes 10 because 10 is reached exactly.
  3. Final Answer:

    'to' excludes end, 'through' includes end if step lands exactly -> Option D
  4. Quick Check:

    'to' excludes, 'through' includes end = C [OK]
Quick Trick: Remember 'to' excludes end, 'through' includes end if exact [OK]
Common Mistakes:
  • Mixing up 'to' and 'through' behavior
  • Assuming both always include or exclude end
  • Ignoring step alignment with end value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes