0
0
Swiftprogramming~10 mins

Labeled statements for nested loops in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Labeled statements for nested loops
Start Outer Loop
Start Inner Loop
Check Condition
Action
Check Break?
Break Outer Loop
End
The outer loop runs, inside it the inner loop runs. A labeled break can exit the outer loop from inside the inner loop.
Execution Sample
Swift
outerLoop: for i in 1...3 {
    for j in 1...3 {
        if i * j == 4 {
            break outerLoop
        }
        print("i=\(i), j=\(j)")
    }
}
This code runs two loops. When i*j equals 4, it breaks out of the outer loop entirely.
Execution Table
StepijCondition i*j==4ActionOutput
1111*1=1 (False)Print i=1, j=1i=1, j=1
2121*2=2 (False)Print i=1, j=2i=1, j=2
3131*3=3 (False)Print i=1, j=3i=1, j=3
4212*1=2 (False)Print i=2, j=1i=2, j=1
5222*2=4 (True)Break outerLoop
6---Exit all loops
💡 At step 5, condition i*j==4 is true, so break outerLoop exits both loops.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
i-11122-
j-12312-
Key Moments - 3 Insights
Why does the break statement exit the outer loop and not just the inner loop?
Because the break uses the label 'outerLoop', it tells Swift to exit that specific loop, not just the inner one. See execution_table step 5 where break outerLoop happens.
What happens if the condition i*j==4 never becomes true?
The inner loop finishes all iterations for each i, and the outer loop continues normally. No labeled break occurs, so loops run fully.
Why is there no output after step 5?
Because the break outerLoop stops all loops immediately, so no further print statements run after step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of j at step 4?
A3
B1
C2
D-
💡 Hint
Check the 'j' column in execution_table row for step 4.
At which step does the condition i*j==4 become true?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Condition i*j==4' column in execution_table.
If we remove the label and just write 'break', what happens when i*j==4?
ACauses a compile error
BBreaks both loops
CBreaks only inner loop and continues outer loop
DSkips current iteration
💡 Hint
Recall that unlabeled break exits the nearest loop only.
Concept Snapshot
Labeled statements let you name loops.
Use 'break label' to exit that loop from inside nested loops.
Syntax: labelName: for ... { ... break labelName ... }
Without label, break exits nearest loop only.
Useful to stop multiple loops at once.
Full Transcript
This visual trace shows how labeled statements work in nested loops in Swift. The outer loop runs from 1 to 3, and inside it the inner loop runs from 1 to 3. At each step, the product i*j is checked. When it equals 4, the break with label 'outerLoop' stops both loops immediately. The execution table shows each step's variables and actions. The variable tracker shows how i and j change. Key moments clarify why the labeled break exits the outer loop, what happens if the condition never triggers, and why no output appears after the break. The quiz tests understanding of variable values at steps, when the condition triggers, and the effect of removing the label. The snapshot summarizes how to use labeled breaks to control nested loops in Swift.