Labeled statements for nested loops in Swift - Time & Space Complexity
When using labeled statements in nested loops, it helps control which loop to break or continue. We want to understand how this affects the total work the program does as input grows.
How does the number of steps change when loops are nested and labeled?
Analyze the time complexity of the following code snippet.
outerLoop: for i in 1...n {
for j in 1...n {
if i * j > n {
break outerLoop
}
print(i, j)
}
}
This code uses a labeled break to exit the outer loop early when a condition is met inside the inner loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The nested loops where the inner loop runs for each outer loop iteration.
- How many times: The inner loop runs fewer times as the product i * j grows, but in the worst case, it can run up to n times for each i.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 to 20 operations before break |
| 100 | About 100 to 200 operations before break |
| 1000 | About 1000 to 2000 operations before break |
Pattern observation: The total steps grow roughly proportional to n, not n squared, because the break stops loops early.
Time Complexity: O(n)
This means the total work grows linearly with the input size because the labeled break stops the loops early.
[X] Wrong: "Nested loops always mean the program runs in n squared time."
[OK] Correct: Because the labeled break can stop loops early, the total steps can be much less than n squared.
Understanding how labeled breaks affect loops shows you can reason about code flow and efficiency, a skill useful in many programming tasks.
"What if we replaced the labeled break with a simple break inside the inner loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand labeled statements
Labeled statements give a name to loops so you can refer to them directly.Step 2: Control flow in nested loops
This naming allows breaking or continuing an outer loop from inside an inner loop without extra flags.Final Answer:
To directly control which loop to break or continue -> Option CQuick Check:
Labeled loops control flow directly = C [OK]
- Thinking labels declare variables
- Assuming labels improve speed
- Confusing labels with memory optimization
Solution
Step 1: Recall label syntax in Swift
Labels are written as a name followed by a colon before the loop statement.Step 2: Match syntax to options
outerLoop: for i in 1...3 { } correctly places 'outerLoop:' before the for loop.Final Answer:
outerLoop: for i in 1...3 { } -> Option BQuick Check:
Label name + colon before loop = A [OK]
- Placing label after the loop
- Using 'label' keyword (not valid)
- Putting label inside loop braces
outerLoop: for i in 1...2 {
for j in 1...3 {
if j == 2 {
break outerLoop
}
print("\(i),\(j)")
}
}Solution
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.Step 2: Trace iterations
First iteration: i=1, j=1 prints "1,1". When j=2, break outerLoop triggers, stopping all loops immediately.Final Answer:
1,1 -> Option DQuick Check:
Break outer loop at j=2 stops all = D [OK]
- Thinking break only stops inner loop
- Expecting all prints before break
- Ignoring label effect on break
outer: for i in 1...2 {
for j in 1...2 {
if j == 2 {
break outerLoop
}
print(i, j)
}
}Solution
Step 1: Check label definition
The loop is labeled 'outer', but break tries to use 'outerLoop' which is undefined.Step 2: Understand label usage
Labels must match exactly. Using a different name causes a compile error.Final Answer:
Label 'outerLoop' is not defined -> Option AQuick Check:
Break label must match defined label = B [OK]
- Using different label names
- Forgetting colon after label
- Thinking break can't be used in nested loops
Solution
Step 1: Understand the stopping condition
We want to stop all loops immediately when i + j == 4, so we must break the outer loop.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.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.Final Answer:
Code that breaks outerLoop when i + j == 4 -> Option AQuick Check:
Labeled break stops all loops = A [OK]
- Using break without label stops only inner loop
- Using continue instead of break
- Not labeling the outer loop
Ready to prove it?
Pass the test and this pattern moves unknown
