0
0
Kotlinprogramming~10 mins

Labeled break and continue in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Labeled break and continue
Start Outer Loop
Start Inner Loop
Check Condition
Labeled break or continue
Exit or Continue Loops
Back to Outer Loop or Exit
The program runs nested loops. Inside, it checks a condition to decide whether to break or continue a specific loop using labels.
Execution Sample
Kotlin
outer@ for (i in 1..3) {
  inner@ for (j in 1..3) {
    if (i == 2 && j == 2) break@outer
    println("i=$i, j=$j")
  }
}
This code uses a labeled break to exit the outer loop when i and j both equal 2.
Execution Table
StepijCondition (i==2 && j==2)ActionOutput
111falsePrint i=1, j=1i=1, j=1
212falsePrint i=1, j=2i=1, j=2
313falsePrint i=1, j=3i=1, j=3
421falsePrint i=2, j=1i=2, j=1
522truebreak@outer (exit outer loop)
6---Exit loops
💡 At step 5, condition is true, so break@outer exits the outer loop and stops all looping.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
i-11122-
j-12312-
Key Moments - 2 Insights
Why does the program stop completely at step 5 instead of just breaking the inner loop?
Because the break uses the label 'outer', it exits the outer loop, not just the inner one. See step 5 in the execution_table where break@outer is used.
What would happen if we used 'continue@outer' instead of 'break@outer' at step 5?
The program would skip the rest of the current outer loop iteration and start the next one. It would not exit all loops. This changes the flow after step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'j' at step 3?
A2
B1
C3
D4
💡 Hint
Check the 'j' column in the execution_table row for step 3.
At which step does the labeled break cause the loops to exit?
AStep 4
BStep 5
CStep 6
DStep 3
💡 Hint
Look at the 'Action' column in the execution_table to find where break@outer happens.
If the condition was never true, how many times would 'println' run?
A9 times
B6 times
C3 times
D0 times
💡 Hint
Consider the nested loops from 1 to 3 for both i and j, and check the output count in execution_table.
Concept Snapshot
Labeled break and continue in Kotlin:
- Use labels to specify which loop to break or continue.
- Syntax: label@ for loops, then break@label or continue@label.
- break@label exits the labeled loop immediately.
- continue@label skips to next iteration of labeled loop.
- Useful in nested loops to control flow precisely.
Full Transcript
This example shows how Kotlin uses labeled break to exit an outer loop from inside an inner loop. The outer loop runs i from 1 to 3, and the inner loop runs j from 1 to 3. When both i and j equal 2, the condition triggers break@outer, which stops all looping immediately. The execution table tracks each step, showing variable values and actions. The variable tracker shows how i and j change over time. Key moments clarify why labeled break exits the outer loop, not just the inner one. The quiz tests understanding of variable values and loop control flow. The concept snapshot summarizes how labeled break and continue work in Kotlin for nested loops.