Bird
0
0

Which labeled statement structure correctly implements this in Swift?

hard📝 Application Q8 of 15
Swift - Loops
You want to print all pairs (i, j) for i and j from 1 to 3, but skip printing pairs where i + j is 4 or more, and stop all loops immediately if i equals 3. Which labeled statement structure correctly implements this in Swift?
Aouter: for i in 1...3 { for j in 1...3 { if i + j >= 4 { continue } if i == 3 { break outer } print("\(i),\(j)") } }
Bouter: for i in 1...3 { for j in 1...3 { if i == 3 { break outer } if i + j >= 4 { continue } print("\(i),\(j)") } }
Cfor i in 1...3 { outer: for j in 1...3 { if i + j >= 4 { continue outer } if i == 3 { break } print("\(i),\(j)") } }
Douter: for i in 1...3 { for j in 1...3 { if i == 3 { continue outer } if i + j >= 4 { break } print("\(i),\(j)") } }
Step-by-Step Solution
Solution:
  1. Step 1: Understand conditions order

    We must break outer loop immediately if i == 3 before printing.
  2. Step 2: Check continue condition

    Skip printing pairs where i + j >= 4 using continue inside inner loop.
  3. Step 3: Verify labeled break usage

    outer: for i in 1...3 { for j in 1...3 { if i == 3 { break outer } if i + j >= 4 { continue } print("\(i),\(j)") } } breaks outer loop when i == 3 before continue or print.
  4. Final Answer:

    Option B correctly implements the logic -> Option B
  5. Quick Check:

    Break outer first, then continue inner [OK]
Quick Trick: Check break conditions before continue and print [OK]
Common Mistakes:
  • Placing break after continue or print
  • Using continue with wrong label
  • Misordering conditions causing wrong output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes