Bird
0
0

Identify the error in this Swift code using labeled statements:

medium📝 Debug Q14 of 15
Swift - Loops
Identify the error in this Swift code using labeled statements:
outer: for i in 1...2 {
    for j in 1...2 {
        if j == 2 {
            break outerLoop
        }
        print(i, j)
    }
}
ALabel 'outerLoop' is not defined
BSyntax error: missing colon after label
CCannot use break inside nested loops
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check label definition

    The loop is labeled 'outer', but break tries to use 'outerLoop' which is undefined.
  2. Step 2: Understand label usage

    Labels must match exactly. Using a different name causes a compile error.
  3. Final Answer:

    Label 'outerLoop' is not defined -> Option A
  4. Quick Check:

    Break label must match defined label = B [OK]
Quick Trick: Break label must exactly match loop label name [OK]
Common Mistakes:
  • Using different label names
  • Forgetting colon after label
  • Thinking break can't be used in nested loops

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes