Bird
0
0

Identify the error in this Swift code snippet:

medium📝 Debug Q14 of 15
Swift - Loops
Identify the error in this Swift code snippet:
for i in stride(from: 1, through: 10, by: 0) {
    print(i)
}
AUsing 'through' instead of 'to' is invalid syntax.
BStep value cannot be zero; it causes a runtime error.
CStart value must be less than end value for positive step.
DThe loop will run infinitely without errors.
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes