0
0
Kotlinprogramming~20 mins

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

Choose your learning style9 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.