What if you could instantly stop all searching loops the moment you find what you want?
Why Labeled statements for nested loops in Swift? - Purpose & Use Cases
Imagine you are searching for a specific item in a big box filled with smaller boxes, and each smaller box has many compartments. You have to check every compartment one by one until you find the item.
Without a clear way to jump out of all the compartments and boxes once you find the item, you end up checking everything even after finding it. This wastes time and makes your code messy with many flags or extra checks.
Labeled statements let you name your loops so you can break out of or continue specific loops directly. This means you can stop searching immediately when you find the item, making your code cleaner and faster.
for i in 1...5 { for j in 1...5 { if condition { break } } }
outerLoop: for i in 1...5 { for j in 1...5 { if condition { break outerLoop } } }
You can control exactly which loop to exit or continue, making nested loops easier to manage and your program more efficient.
When scanning a grid for a treasure, labeled statements let you stop searching all rows and columns as soon as you find the treasure, instead of checking every cell.
Manual nested loops can be hard to control and inefficient.
Labeled statements give names to loops for precise control.
This makes breaking out of nested loops simple and clean.
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
