Bird
0
0

You want to print all even numbers from 1 to 10 but skip printing 6. Which Swift code snippet correctly uses continue and break to achieve this?

hard📝 Application Q8 of 15
Swift - Loops
You want to print all even numbers from 1 to 10 but skip printing 6. Which Swift code snippet correctly uses continue and break to achieve this?
Afor i in 1...10 { if i % 2 == 0 { if i == 6 { continue } print(i) } }
Bfor i in 1...10 { if i == 6 { continue } print(i) }
Cfor i in 1...10 { if i % 2 == 0 { if i == 6 { break } print(i) } }
Dfor i in 1...10 { if i == 6 { break } if i % 2 == 0 { print(i) } }
Step-by-Step Solution
Solution:
  1. Step 1: Understand requirements

    Print even numbers 1 to 10, but skip printing 6 only.
  2. Step 2: Analyze options

    for i in 1...10 { if i % 2 == 0 { if i == 6 { continue } print(i) } } uses continue to skip printing 6 inside even number check, which is correct. for i in 1...10 { if i == 6 { break } if i % 2 == 0 { print(i) } } breaks loop at 6, stopping early. for i in 1...10 { if i == 6 { continue } print(i) } skips 6 but prints odd numbers. for i in 1...10 { if i % 2 == 0 { if i == 6 { break } print(i) } } breaks inside even check, stopping early.
  3. Final Answer:

    skips printing 6 and prints other even numbers -> Option A
  4. Quick Check:

    Use continue to skip 6, break stops loop early [OK]
Quick Trick: Use continue to skip specific iteration, break to stop loop [OK]
Common Mistakes:
  • Using break instead of continue to skip 6
  • Skipping odd numbers unintentionally
  • Stopping loop too early

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes