What if you could instantly jump out of any loop you want, no matter how deep it is?
Why Labeled break and continue in Kotlin? - Purpose & Use Cases
Imagine you are cooking multiple dishes at once, and you need to stop cooking one dish immediately when a certain ingredient is missing, but continue cooking the others. Doing this without clear signals can get confusing fast.
Without labeled break and continue, you might have to write complicated checks and flags to control which loop to stop or continue. This makes your code messy, hard to read, and easy to make mistakes.
Labeled break and continue let you clearly say which loop to stop or skip to the next iteration. This keeps your code clean and your intentions clear, just like using labels on your cooking pots to know exactly which one to handle.
for (i in 1..3) { for (j in 1..3) { if (someCondition) break } }
outer@ for (i in 1..3) { for (j in 1..3) { if (someCondition) break@outer } }
You can precisely control complex nested loops, making your code easier to understand and maintain.
When searching for a specific item in a grid, labeled break lets you stop searching immediately once found, without extra checks.
Labeled break and continue help manage nested loops clearly.
They prevent messy and error-prone manual checks.
They make your code cleaner and easier to follow.
Practice
break@label statement do in Kotlin nested loops?Solution
Step 1: Understand labeled break usage
Thebreak@labelstatement is used to exit a specific loop marked by the label, not just the innermost loop.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.Final Answer:
It stops the loop identified by the label immediately. -> Option AQuick Check:
break@label stops labeled loop [OK]
- Confusing break@label with continue@label
- Thinking break@label only stops innermost loop
- Assuming break@label pauses instead of stops
outerLoop in Kotlin?Solution
Step 1: Recall Kotlin labeled continue syntax
Kotlin uses the@symbol to refer to labels with break or continue, so the correct syntax iscontinue@label.Step 2: Eliminate incorrect options
Options B, C, and D use invalid syntax for labels in Kotlin.Final Answer:
continue@outerLoop -> Option DQuick Check:
Use continue@label to continue labeled loop [OK]
- Omitting @ symbol before label
- Using parentheses or other symbols with label
- Confusing syntax with other languages
outer@ for (i in 1..3) {
inner@ for (j in 1..3) {
if (i == 2 && j == 2) break@outer
print("$i$j ")
}
}Solution
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@outerstops the outer loop entirely.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".Final Answer:
11 12 13 21 -> Option BQuick Check:
break@outer stops all loops at i=2,j=2 [OK]
- Assuming break@outer only stops inner loop
- Including values after break condition
- Confusing break@outer with continue@outer
outer@ for (i in 1..2) {
for (j in 1..2) {
if (i == 1) continue outer
println("i=$i, j=$j")
}
}Solution
Step 1: Check labeled continue syntax
Kotlin requires the @ symbol when using labels with break or continue, so it must becontinue@outer.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.Final Answer:
continue@outer should be used instead of continue outer. -> Option AQuick Check:
Use continue@label syntax to avoid errors [OK]
- Omitting @ symbol in labeled continue
- Thinking label must be on inner loop
- Assuming no error when syntax is wrong
outer and inner, how can you skip only the current iteration of the outer loop when a condition in the inner loop is met?Solution
Step 1: Understand labeled continue effect
Usingcontinue@outerskips the current iteration of the outer loop, moving to the next iteration of outer.Step 2: Compare with break and unlabeled continue
break@outerwould exit the outer loop entirely, not just skip iteration. Unlabeled continue affects only inner loop.Final Answer:
Use continue@outer inside inner loop. -> Option CQuick Check:
continue@outer skips outer loop iteration [OK]
- Using break@outer instead of continue@outer
- Using unlabeled continue to skip outer loop
- Confusing break and continue effects
Ready to prove it?
Pass the test and this pattern moves unknown
