Bird
Raised Fist0
Swiftprogramming~20 mins

Labeled statements for nested loops in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Nested Loops Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested loops with labeled break
What is the output of this Swift code using labeled break in nested loops?
Swift
outerLoop: for i in 1...3 {
    for j in 1...3 {
        if i * j == 4 {
            break outerLoop
        }
        print("\(i),\(j)")
    }
}
A
1,1
1,2
1,3
2,1
2,2
B
1,1
1,2
2,1
2,2
3,1
C
1,1
1,2
1,3
2,1
2,2
3,1
3,2
3,3
D
1,1
1,2
1,3
2,1
Attempts:
2 left
💡 Hint
Remember that break with a label exits the entire labeled loop, not just the inner loop.
❓ Predict Output
intermediate
2:00remaining
Output of nested loops with labeled continue
What is the output of this Swift code using labeled continue in nested loops?
Swift
outer: for i in 1...2 {
    for j in 1...3 {
        if j == 2 {
            continue outer
        }
        print("\(i),\(j)")
    }
}
A
1,1
2,1
B
1,1
1,3
2,1
2,3
C
1,1
2,1
2,3
D
1,1
1,3
2,1
2,3
1,2
2,2
Attempts:
2 left
💡 Hint
Labeled continue skips the rest of the current iteration of the labeled loop.
🧠 Conceptual
advanced
1:30remaining
Purpose of labeled statements in nested loops
Why are labeled statements useful in nested loops in Swift?
AThey allow breaking or continuing an outer loop directly from an inner loop.
BThey improve the performance of nested loops by optimizing memory.
CThey automatically parallelize nested loops for faster execution.
DThey replace the need for functions inside loops.
Attempts:
2 left
💡 Hint
Think about controlling which loop to break or continue when loops are inside each other.
🔧 Debug
advanced
1:30remaining
Identify the error in labeled break usage
What error occurs when running this Swift code?
Swift
for i in 1...3 {
    innerLoop: for j in 1...3 {
        if i == j {
            break outerLoop
        }
        print(i, j)
    }
}
ARuntime error: index out of range
BCompile-time error: 'outerLoop' label not found
CNo error, prints pairs until i == j
DCompile-time error: missing colon after label
Attempts:
2 left
💡 Hint
Check if the label used in break exists in the code.
❓ Predict Output
expert
2:00remaining
Count iterations with labeled continue in nested loops
What is the value of 'count' after running this Swift code?
Swift
var count = 0
outer: for i in 1...3 {
    for j in 1...3 {
        if i + j == 4 {
            continue outer
        }
        count += 1
    }
}
A5
B7
C3
D9
Attempts:
2 left
💡 Hint
Each time i+j equals 4, the code skips to the next i, skipping remaining j values.

Practice

(1/5)
1. What is the main purpose of using labeled statements in nested loops in Swift?
easy
A. To speed up the loop execution
B. To declare variables inside loops
C. To directly control which loop to break or continue
D. To automatically optimize memory usage

Solution

  1. Step 1: Understand labeled statements

    Labeled statements give a name to loops so you can refer to them directly.
  2. Step 2: Control flow in nested loops

    This naming allows breaking or continuing an outer loop from inside an inner loop without extra flags.
  3. Final Answer:

    To directly control which loop to break or continue -> Option C
  4. Quick Check:

    Labeled loops control flow directly = C [OK]
Hint: Labels let you break/continue outer loops easily [OK]
Common Mistakes:
  • Thinking labels declare variables
  • Assuming labels improve speed
  • Confusing labels with memory optimization
2. Which of the following is the correct syntax to label a loop named 'outerLoop' in Swift?
easy
A. for outerLoop in 1...3 { }
B. outerLoop: for i in 1...3 { }
C. for i in 1...3 outerLoop: { }
D. label outerLoop for i in 1...3 { }

Solution

  1. Step 1: Recall label syntax in Swift

    Labels are written as a name followed by a colon before the loop statement.
  2. Step 2: Match syntax to options

    outerLoop: for i in 1...3 { } correctly places 'outerLoop:' before the for loop.
  3. Final Answer:

    outerLoop: for i in 1...3 { } -> Option B
  4. Quick Check:

    Label name + colon before loop = A [OK]
Hint: Label name ends with colon before loop starts [OK]
Common Mistakes:
  • Placing label after the loop
  • Using 'label' keyword (not valid)
  • Putting label inside loop braces
3. What will be the output of the following Swift code?
outerLoop: for i in 1...2 {
    for j in 1...3 {
        if j == 2 {
            break outerLoop
        }
        print("\(i),\(j)")
    }
}
medium
A. 1,1 1,2 1,3 2,1 2,2 2,3
B. 1,1 1,2
C. 1,1 2,1
D. 1,1

Solution

  1. Step 1: Understand loop and break behavior

    The outer loop runs i from 1 to 2, inner loop j from 1 to 3. When j == 2, break outerLoop stops the outer loop entirely.
  2. Step 2: Trace iterations

    First iteration: i=1, j=1 prints "1,1". When j=2, break outerLoop triggers, stopping all loops immediately.
  3. Final Answer:

    1,1 -> Option D
  4. Quick Check:

    Break outer loop at j=2 stops all = D [OK]
Hint: Break with label stops outer loop immediately [OK]
Common Mistakes:
  • Thinking break only stops inner loop
  • Expecting all prints before break
  • Ignoring label effect on break
4. 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)
    }
}
medium
A. Label 'outerLoop' is not defined
B. Syntax error: missing colon after label
C. Cannot use break inside nested loops
D. No error, code runs fine

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]
Hint: 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
5. Given the task to print all pairs (i, j) where i and j range from 1 to 3, but stop all loops immediately when i + j equals 4, which Swift code correctly uses labeled statements to achieve this?
hard
A. outerLoop: for i in 1...3 { for j in 1...3 { if i + j == 4 { break outerLoop } print("\(i),\(j)") } }
B. for i in 1...3 { for j in 1...3 { if i + j == 4 { break } print(i, j) } }
C. outerLoop: for i in 1...3 { for j in 1...3 { if i + j == 4 { continue outerLoop } print(i, j) } }
D. for i in 1...3 { for j in 1...3 { if i + j == 4 { continue } print(i, j) } }

Solution

  1. Step 1: Understand the stopping condition

    We want to stop all loops immediately when i + j == 4, so we must break the outer loop.
  2. Step 2: Use labeled break correctly

    outerLoop: for i in 1...3 { for j in 1...3 { if i + j == 4 { break outerLoop } print("\(i),\(j)") } } uses 'break outerLoop' to stop the outer loop from inside the inner loop, which is correct.
  3. Step 3: Check other options

    for i in 1...3 { for j in 1...3 { if i + j == 4 { break } print(i, j) } } breaks only inner loop; C uses continue which skips iterations but doesn't stop loops; D continues inner loop only.
  4. Final Answer:

    Code that breaks outerLoop when i + j == 4 -> Option A
  5. Quick Check:

    Labeled break stops all loops = A [OK]
Hint: Use labeled break to stop outer loop from inner loop [OK]
Common Mistakes:
  • Using break without label stops only inner loop
  • Using continue instead of break
  • Not labeling the outer loop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard