Bird
0
0

Find the error in this Swift code snippet:

medium📝 Debug Q14 of 15
Swift - Loops
Find the error in this Swift code snippet:
for i in 1...5 {
    if i == 3 {
        continue
    }
    if i == 6 {
        break
    }
    print(i)
}
Abreak should come before continue in the code.
Bcontinue cannot be used inside a for loop.
CThe loop will never break because i never reaches 6.
DThe range 1...5 is invalid.
Step-by-Step Solution
Solution:
  1. Step 1: Check the loop range and conditions

    The loop runs from 1 to 5, so i will never be 6.
  2. Step 2: Analyze the break condition

    Since i == 6 never occurs, the break statement will never execute, so the loop runs fully except skipping 3 due to continue.
  3. Final Answer:

    The loop will never break because i never reaches 6. -> Option C
  4. Quick Check:

    Break condition unreachable [OK]
Quick Trick: Check if break condition can ever be true [OK]
Common Mistakes:
  • Thinking continue is invalid in for loops
  • Assuming break runs before continue always
  • Not checking loop range carefully

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes