Bird
Raised Fist0
Kotlinprogramming~15 mins

Labeled break and continue in Kotlin - Deep Dive

Choose your learning style10 modes available
Overview - Labeled break and continue
What is it?
Labeled break and continue are special commands in Kotlin that let you control loops more precisely. They allow you to stop or skip iterations not just in the nearest loop but in an outer loop by using labels. Labels are names you give to loops, so you can tell the program exactly which loop to affect. This helps when you have loops inside loops and want to jump out or skip steps in a specific one.
Why it matters
Without labeled break and continue, you can only affect the closest loop, which makes controlling nested loops tricky and messy. This can lead to complicated code or bugs when you want to stop or skip outer loops early. Labeled break and continue make your code clearer and easier to manage when dealing with multiple loops, saving time and reducing errors.
Where it fits
Before learning labeled break and continue, you should understand basic loops (for, while) and simple break and continue statements. After this, you can explore more advanced flow control like return with labels, inline functions, and coroutines for complex program flows.
Mental Model
Core Idea
Labeled break and continue let you jump out of or skip iterations in specific loops by naming them, giving you precise control over nested loops.
Think of it like...
Imagine you are in a building with many floors (loops), and you want to exit or skip a floor not just the one you are currently on. Labels are like floor numbers, so you can say 'skip floor 3' or 'exit floor 2' directly, instead of only being able to act on the floor you stand on.
┌───────────────┐
│ Outer Loop    │  ← label@outer
│  ┌─────────┐  │
│  │ Inner   │  │
│  │ Loop    │  │
│  └─────────┘  │
└───────────────┘

Commands:
- break@outer: exit Outer Loop completely
- continue@outer: skip to next iteration of Outer Loop
- break: exit Inner Loop only
- continue: skip to next iteration of Inner Loop
Build-Up - 7 Steps
1
FoundationBasic loops and control statements
🤔
Concept: Introduce simple loops and how break and continue work without labels.
In Kotlin, loops like for and while repeat code. The break statement stops the loop early. The continue statement skips the current step and moves to the next iteration. Example: for (i in 1..5) { if (i == 3) break println(i) } Output: 1 2 This stops the loop when i is 3.
Result
The loop prints numbers 1 and 2, then stops at 3.
Understanding simple break and continue is the base for controlling loops before adding labels.
2
FoundationNested loops basics
🤔
Concept: Show how loops inside loops work and how break/continue affect only the nearest loop.
You can put one loop inside another. The inner loop runs completely for each outer loop step. Example: for (i in 1..3) { for (j in 1..3) { if (j == 2) break println("i=$i, j=$j") } } Output: i=1, j=1 i=2, j=1 i=3, j=1 The break stops only the inner loop when j is 2, but the outer loop continues.
Result
Inner loop stops early each time, outer loop continues all iterations.
Break and continue without labels only affect the closest loop, which can limit control in nested loops.
3
IntermediateIntroducing labels for loops
🤔
Concept: Explain how to name loops with labels to target them specifically.
You can give a loop a label by writing a name followed by @ before the loop. Example: outer@ for (i in 1..3) { for (j in 1..3) { println("i=$i, j=$j") } } Here, 'outer' is a label for the outer loop. You can use this label to control it later.
Result
Loops run normally but now have a name to refer to.
Labels let you name loops so you can control them directly, which is key for nested loop management.
4
IntermediateUsing labeled break to exit outer loops
🤔Before reading on: do you think break@label exits only the inner loop or the labeled outer loop? Commit to your answer.
Concept: Show how break with a label exits the named loop, not just the nearest one.
Using break with a label lets you stop an outer loop from inside an inner loop. Example: outer@ for (i in 1..3) { for (j in 1..3) { if (j == 2) break@outer println("i=$i, j=$j") } } Output: i=1, j=1 The break@outer stops the outer loop completely when j is 2.
Result
Only one line prints before both loops stop.
Labeled break lets you jump out of multiple nested loops at once, simplifying complex loop exits.
5
IntermediateUsing labeled continue to skip outer loop iterations
🤔Before reading on: does continue@label skip the current iteration of the labeled loop or the inner loop? Commit to your answer.
Concept: Explain how continue with a label skips to the next iteration of the named loop.
Using continue with a label skips the rest of the current iteration of the labeled loop and moves to its next iteration. Example: outer@ for (i in 1..3) { for (j in 1..3) { if (j == 2) continue@outer println("i=$i, j=$j") } } Output: i=1, j=1 i=3, j=1 i=3, j=3 When j is 2, continue@outer skips to the next i value, skipping some inner loop steps.
Result
Some inner loop iterations are skipped, outer loop moves on early.
Labeled continue gives precise control to skip outer loop steps from inside inner loops.
6
AdvancedCombining labeled break and continue in complex loops
🤔Before reading on: can you use both labeled break and continue in the same nested loop structure? Predict how they interact.
Concept: Show how labeled break and continue can be combined to manage complex nested loops cleanly.
You can use both labeled break and continue to control loops precisely. Example: outer@ for (i in 1..3) { inner@ for (j in 1..3) { if (j == 2 && i == 1) continue@outer if (j == 3 && i == 2) break@outer println("i=$i, j=$j") } } Output: i=1, j=1 i=3, j=1 i=3, j=2 Here, continue@outer skips to next i when j=2 and i=1, break@outer stops all loops when j=3 and i=2.
Result
Loops skip and stop at different points based on conditions.
Mastering both labeled break and continue lets you write clear, efficient nested loops without flags or extra variables.
7
ExpertPerformance and readability trade-offs with labels
🤔Before reading on: do you think using many labels improves or harms code readability and performance? Commit to your answer.
Concept: Discuss when using labeled break and continue helps or hurts code quality and performance.
While labeled break and continue improve control, overusing them can make code hard to read and maintain. They do not affect performance significantly but can confuse readers if labels are unclear or nested too deeply. Best practice is to use labels sparingly and name them clearly. Sometimes refactoring loops into functions or using other control structures is better. Example of confusing code: loop1@ for (i in 1..5) { loop2@ for (j in 1..5) { loop3@ for (k in 1..5) { if (k == 3) break@loop1 } } } This is hard to follow.
Result
Code can become confusing despite precise control.
Knowing when to use labels prevents messy code and encourages maintainable, clear programs.
Under the Hood
Kotlin compiles labeled break and continue into bytecode that uses jump instructions to exit or continue the specified loop. Labels are markers attached to loops, and the compiler translates break@label and continue@label into jumps targeting those markers. This avoids nested flag variables or complex condition checks, making control flow explicit and efficient.
Why designed this way?
Labeled break and continue were designed to solve the problem of controlling nested loops cleanly without extra variables or complicated logic. Other languages like Java have similar features, but Kotlin's syntax is concise and expressive. The design balances power and readability, allowing precise control while keeping code understandable.
┌───────────────┐
│ outer loop    │  ← label@outer
│  ┌─────────┐  │
│  │ inner   │  │
│  │ loop    │  │
│  └─────────┘  │
└───────────────┘

Execution flow:
[inner loop] --break@outer--> jump out of outer loop
[inner loop] --continue@outer--> jump to next outer loop iteration
[inner loop] --break--> jump out of inner loop
[inner loop] --continue--> jump to next inner loop iteration
Myth Busters - 4 Common Misconceptions
Quick: Does break@label only stop the nearest loop or the labeled loop? Commit to your answer.
Common Belief:break@label only stops the nearest loop, just like break without a label.
Tap to reveal reality
Reality:break@label stops the loop with the specified label, which can be an outer loop, not just the nearest one.
Why it matters:Believing this causes confusion and bugs when trying to exit outer loops, leading to unexpected loop continuation.
Quick: Does continue@label skip the current iteration of the inner loop or the labeled loop? Commit to your answer.
Common Belief:continue@label behaves exactly like continue without a label, affecting only the inner loop.
Tap to reveal reality
Reality:continue@label skips the current iteration of the labeled loop, which can be an outer loop, not just the inner one.
Why it matters:Misunderstanding this leads to incorrect loop skipping and logic errors in nested loops.
Quick: Can you use labels anywhere in Kotlin code or only on loops? Commit to your answer.
Common Belief:Labels can be used on any statement or block in Kotlin.
Tap to reveal reality
Reality:Labels are primarily used on loops and lambda expressions; using them elsewhere is limited or invalid.
Why it matters:Trying to label unsupported code blocks causes syntax errors and confusion.
Quick: Does using many labels always improve code clarity? Commit to your answer.
Common Belief:More labels always make nested loops easier to understand and control.
Tap to reveal reality
Reality:Excessive labels can make code harder to read and maintain, especially with unclear or similar names.
Why it matters:Overusing labels can lead to confusing code, defeating their purpose of clarity.
Expert Zone
1
Labels can also be used with lambda expressions to control returns and continue-like behavior inside lambdas.
2
Using descriptive label names improves readability, especially in deeply nested loops, avoiding generic names like 'loop1'.
3
Labeled break and continue do not add runtime overhead; they compile to efficient jump instructions, so performance is not a concern.
When NOT to use
Avoid labeled break and continue when loops are simple or when nested loops can be refactored into separate functions. In some cases, using flags or higher-order functions like 'any' or 'all' can express logic more clearly. Also, avoid labels if they make code harder to read or maintain.
Production Patterns
In production Kotlin code, labeled break and continue are used to simplify complex nested loops, such as parsing nested data structures or processing multi-level menus. They help avoid extra variables and nested ifs. Clear label naming and minimal nesting are best practices. Sometimes, developers combine labels with inline functions for cleaner flow control.
Connections
Exception handling
Both labeled break/continue and exceptions provide ways to jump out of normal code flow.
Understanding labeled jumps helps grasp how exceptions can interrupt and redirect program flow beyond local scopes.
State machines
Labeled break and continue can implement state transitions in nested loops, similar to state machine steps.
Recognizing loops as states and labeled jumps as transitions clarifies complex control flows in programs.
Traffic control systems
Like traffic signals directing cars at intersections, labeled break and continue direct program flow at loop intersections.
Seeing program loops as traffic routes helps understand the need for precise control commands to avoid collisions or deadlocks.
Common Pitfalls
#1Trying to use break or continue with a label that does not exist.
Wrong approach:break@missingLabel for (i in 1..3) { println(i) }
Correct approach:myLabel@ for (i in 1..3) { if (i == 2) break@myLabel println(i) }
Root cause:Labels must be declared on loops before they can be referenced; missing or misspelled labels cause errors.
#2Using break or continue without labels inside nested loops when intending to affect outer loops.
Wrong approach:for (i in 1..3) { for (j in 1..3) { if (j == 2) break println("i=$i, j=$j") } }
Correct approach:outer@ for (i in 1..3) { for (j in 1..3) { if (j == 2) break@outer println("i=$i, j=$j") } }
Root cause:Without labels, break only exits the nearest loop, not outer loops, leading to unexpected behavior.
#3Overusing labels with unclear names causing confusing code.
Wrong approach:loop1@ for (i in 1..3) { loop2@ for (j in 1..3) { loop3@ for (k in 1..3) { if (k == 2) break@loop1 } } }
Correct approach:parsing@ for (i in 1..3) { row@ for (j in 1..3) { cell@ for (k in 1..3) { if (k == 2) break@parsing } } }
Root cause:Generic label names do not convey meaning, making code hard to read and maintain.
Key Takeaways
Labeled break and continue give you precise control over nested loops by naming the loops you want to affect.
Without labels, break and continue only affect the nearest loop, which limits control in complex nested structures.
Using labels wisely can simplify code and avoid extra variables or complicated logic for loop control.
Overusing or poorly naming labels can make code confusing, so use them sparingly and clearly.
Understanding labeled break and continue helps you write clearer, more maintainable Kotlin programs with complex loops.

Practice

(1/5)
1. What does the break@label statement do in Kotlin nested loops?
easy
A. It stops the loop identified by the label immediately.
B. It skips the current iteration of the innermost loop.
C. It restarts the entire program from the beginning.
D. It pauses the loop until a condition is met.

Solution

  1. Step 1: Understand labeled break usage

    The break@label statement is used to exit a specific loop marked by the label, not just the innermost loop.
  2. Step 2: Compare with other options

    Options B and D describe continue or pause behavior, which is incorrect. It restarts the entire program from the beginning. is unrelated to loop control.
  3. Final Answer:

    It stops the loop identified by the label immediately. -> Option A
  4. Quick Check:

    break@label stops labeled loop [OK]
Hint: break@label exits the labeled loop immediately [OK]
Common Mistakes:
  • Confusing break@label with continue@label
  • Thinking break@label only stops innermost loop
  • Assuming break@label pauses instead of stops
2. Which of the following is the correct syntax to continue the outer loop labeled outerLoop in Kotlin?
easy
A. continue#outerLoop
B. continue outerLoop
C. continue(outerLoop)
D. continue@outerLoop

Solution

  1. Step 1: Recall Kotlin labeled continue syntax

    Kotlin uses the @ symbol to refer to labels with break or continue, so the correct syntax is continue@label.
  2. Step 2: Eliminate incorrect options

    Options B, C, and D use invalid syntax for labels in Kotlin.
  3. Final Answer:

    continue@outerLoop -> Option D
  4. Quick Check:

    Use continue@label to continue labeled loop [OK]
Hint: Use @ symbol with label for continue or break [OK]
Common Mistakes:
  • Omitting @ symbol before label
  • Using parentheses or other symbols with label
  • Confusing syntax with other languages
3. What is the output of the following Kotlin code?
outer@ for (i in 1..3) {
    inner@ for (j in 1..3) {
        if (i == 2 && j == 2) break@outer
        print("$i$j ")
    }
}
medium
A. 11 12 13 21 22
B. 11 12 13 21
C. 11 12 13 21 22 23
D. 11 12 13

Solution

  1. Step 1: Trace nested loops and break condition

    The outer loop runs i=1 to 3, inner loop j=1 to 3. When i=2 and j=2, break@outer stops the outer loop entirely.
  2. Step 2: List printed values before break

    For i=1, j=1..3 prints 11 12 13. For i=2, j=1 prints 21, then at j=2 break@outer stops all loops. So output is "11 12 13 21".
  3. Final Answer:

    11 12 13 21 -> Option B
  4. Quick Check:

    break@outer stops all loops at i=2,j=2 [OK]
Hint: break@label stops all loops at labeled point [OK]
Common Mistakes:
  • Assuming break@outer only stops inner loop
  • Including values after break condition
  • Confusing break@outer with continue@outer
4. Identify the error in this Kotlin code snippet:
outer@ for (i in 1..2) {
    for (j in 1..2) {
        if (i == 1) continue outer
        println("i=$i, j=$j")
    }
}
medium
A. continue@outer should be used instead of continue outer.
B. continue cannot be used with a label on an inner loop.
C. Label 'outer' is not defined.
D. No error; code runs correctly.

Solution

  1. Step 1: Check labeled continue syntax

    Kotlin requires the @ symbol when using labels with break or continue, so it must be continue@outer.
  2. Step 2: Verify label definition and usage

    The label 'outer' is correctly defined, so Label 'outer' is not defined. is wrong. Using continue with label without @ causes syntax error.
  3. Final Answer:

    continue@outer should be used instead of continue outer. -> Option A
  4. Quick Check:

    Use continue@label syntax to avoid errors [OK]
Hint: Always use @ with label in continue or break [OK]
Common Mistakes:
  • Omitting @ symbol in labeled continue
  • Thinking label must be on inner loop
  • Assuming no error when syntax is wrong
5. Given nested loops labeled outer and inner, how can you skip only the current iteration of the outer loop when a condition in the inner loop is met?
hard
A. Use break@outer inside inner loop.
B. Use continue without label inside inner loop.
C. Use continue@outer inside inner loop.
D. Use break without label inside inner loop.

Solution

  1. Step 1: Understand labeled continue effect

    Using continue@outer skips the current iteration of the outer loop, moving to the next iteration of outer.
  2. Step 2: Compare with break and unlabeled continue

    break@outer would exit the outer loop entirely, not just skip iteration. Unlabeled continue affects only inner loop.
  3. Final Answer:

    Use continue@outer inside inner loop. -> Option C
  4. Quick Check:

    continue@outer skips outer loop iteration [OK]
Hint: Use continue@label to skip labeled loop iteration [OK]
Common Mistakes:
  • Using break@outer instead of continue@outer
  • Using unlabeled continue to skip outer loop
  • Confusing break and continue effects
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