Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Loops
What will be the output of this Swift code?
outer: for i in 1...2 {
  for j in 1...2 {
    if i == 2 { break outer }
    print("\(i),\(j)")
  }
}
A1,1 1,2
B1,1 1,2 2,1 2,2
C2,1 2,2
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the loops and condition

    Outer loop runs i=1 to 2; inner loop j=1 to 2. When i==2, break outer loop.
  2. Step 2: Trace output

    For i=1, j=1 and j=2 print "1,1" and "1,2". When i=2, break outer loop immediately, so no print.
  3. Final Answer:

    1,1 1,2 -> Option A
  4. Quick Check:

    Labeled break exits outer loop early [OK]
Quick Trick: Break with label exits outer loop immediately [OK]
Common Mistakes:
  • Ignoring labeled break and printing all values
  • Confusing break with continue
  • Expecting output for i=2

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes