Bird
Raised Fist0
Kotlinprogramming~20 mins

Labeled break and continue in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Labeled Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of labeled break in nested loops
What is the output of this Kotlin code using a labeled break?
Kotlin
fun main() {
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (i * j > 3) break@outer
            print("$i$j ")
        }
    }
}
A11 12 13 21
B11 12 21 22 23
C11 12 13 21 22 23 31 32 33
D11 12 21 22 31 32
Attempts:
2 left
💡 Hint
The labeled break exits the outer loop when the condition is met.
❓ Predict Output
intermediate
2:00remaining
Output of labeled continue in nested loops
What does this Kotlin code print when using a labeled continue?
Kotlin
fun main() {
    outer@ for (i in 1..2) {
        for (j in 1..3) {
            if (j == 2) continue@outer
            print("$i$j ")
        }
    }
}
A11 21 22 23
B11 21
C11 12 13 21 22 23
D11 13 21 23
Attempts:
2 left
💡 Hint
The labeled continue skips to the next iteration of the outer loop when j == 2.
🔧 Debug
advanced
2:00remaining
Identify the error with labeled break usage
What error does this Kotlin code produce?
Kotlin
fun main() {
    for (i in 1..3) {
        inner@ for (j in 1..3) {
            if (i == 2) break@outer
            print("$i$j ")
        }
    }
}
AUnresolved reference: outer
BSyntax error: Missing colon after label
CNo error, prints 11 12 13 21 22 23 31 32 33
DRuntime exception: Label not found
Attempts:
2 left
💡 Hint
Check if the label used in break exists in the code.
📝 Syntax
advanced
2:00remaining
Which option correctly uses labeled continue?
Which of these Kotlin code snippets correctly uses labeled continue to skip the current iteration of the outer loop?
Aouter@ for (i in 1..2) { for (j in 1..2) { if (j == 2) continue@ else print("$i$j ") } }
Bouter@ for (i in 1..2) { for (j in 1..2) { if (j == 2) continue outer else print("$i$j ") } }
Couter@ for (i in 1..2) { for (j in 1..2) { if (j == 2) continue@outer else print("$i$j ") } }
Douter@ for (i in 1..2) { for (j in 1..2) { if (j == 2) continue@inner else print("$i$j ") } }
Attempts:
2 left
💡 Hint
Labeled continue syntax requires '@' after the label name.
🚀 Application
expert
2:00remaining
Count iterations skipped by labeled continue
Given this Kotlin code, what value is printed by println(count)?
Kotlin
fun main() {
    var count = 0
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            count++
            if (i + j == 4) continue@outer
        }
    }
    println(count)
}
A8
B7
C9
D6
Attempts:
2 left
💡 Hint
The continue@outer skips the rest of the inner loop and moves to next i when i+j == 4.

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