Bird
0
0

Which of the following correctly creates a stride in Swift that counts down from 10 to 0 by 2?

easy📝 Conceptual Q2 of 15
Swift - Loops
Which of the following correctly creates a stride in Swift that counts down from 10 to 0 by 2?
Astride(from: 10, to: 0, by: -2)
Bstride(from: 10, through: 0, by: -2)
Cstride(from: 0, to: 10, by: 2)
Dstride(from: 0, through: 10, by: -2)
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct parameters for descending stride

    To count down from 10 to 0 by 2, start is 10, step is -2, and end is 0. Using through includes 0 if reached.
  2. Step 2: Check each option

    stride(from: 10, through: 0, by: -2) uses stride(from: 10, through: 0, by: -2), which correctly counts 10, 8, 6, 4, 2, 0. stride(from: 10, to: 0, by: -2) uses to: 0 which excludes 0. Options B and C start at 0, which is incorrect.
  3. Final Answer:

    stride(from: 10, through: 0, by: -2) correctly counts down including 0 -> Option B
  4. Quick Check:

    Descending stride with 'through' includes end = D [OK]
Quick Trick: Use negative step and 'through' to include end in descending stride [OK]
Common Mistakes:
  • Using 'to' instead of 'through' and missing the end value
  • Using positive step for descending sequence
  • Swapping start and end values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes