0
0
Kotlinprogramming~10 mins

Labeled break and continue in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to break out of the outer loop using a label.

Kotlin
outer@ for (i in 1..3) {
    for (j in 1..3) {
        if (i == 2 && j == 2) {
            [1]@outer
        }
        println("i = $i, j = $j")
    }
}
Drag options to blanks, or click blank then click option'
Abreak
Bcontinue
Creturn
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using continue instead of break causes the loop to skip to next iteration.
Using return exits the function, not just the loop.
2fill in blank
medium

Complete the code to continue the outer loop when a condition is met.

Kotlin
outer@ for (i in 1..3) {
    for (j in 1..3) {
        if (i == 1 && j == 2) {
            [1]@outer
        }
        println("i = $i, j = $j")
    }
}
Drag options to blanks, or click blank then click option'
Areturn
Bbreak
Ccontinue
Dskip
Attempts:
3 left
💡 Hint
Common Mistakes
Using break exits the loop instead of continuing.
Using return exits the function, not just the loop.
3fill in blank
hard

Fix the error in the code to correctly use a labeled continue.

Kotlin
loop@ for (x in 1..3) {
    for (y in 1..3) {
        if (x == 2 && y == 1) {
            [1]@loop
        }
        println("x = $x, y = $y")
    }
}
Drag options to blanks, or click blank then click option'
Acontinue
Bexit
Cskip
Dbreak
Attempts:
3 left
💡 Hint
Common Mistakes
Using break instead of continue causes the loop to exit.
Using undefined keywords like skip or exit causes errors.
4fill in blank
hard

Fill both blanks to create a labeled break and continue in nested loops.

Kotlin
outer@ for (a in 1..3) {
    inner@ for (b in 1..3) {
        if (a == 2 && b == 2) {
            [1]@outer
        }
        if (a == 1 && b == 3) {
            [2]@inner
        }
        println("a = $a, b = $b")
    }
}
Drag options to blanks, or click blank then click option'
Abreak
Bcontinue
Creturn
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up break and continue keywords.
Using return or exit which are not valid for loops.
5fill in blank
hard

Fill all three blanks to use labeled break and continue correctly in nested loops with conditions.

Kotlin
loop1@ for (i in 1..3) {
    loop2@ for (j in 1..3) {
        if (i == 3 && j == 1) {
            [1]@loop1
        }
        if (i == 2 && j == 3) {
            [2]@loop2
        }
        println("i = $i, j = $j")
        if (i == 1 && j == 2) {
            [3]@loop2
        }
    }
}
Drag options to blanks, or click blank then click option'
Abreak
Bcontinue
Creturn
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using return or exit which are not valid loop controls.
Confusing which label to use with break or continue.