kotlin labels
fun main() {
loop@ for (i in 1..5) {
for (j in 1..5) {
if (i == 3 && j == 3) {
println("Breaking the loop")
break@loop
}
println("i = $i, j = $j")
}
}
}
The code snippet uses Kotlin's label feature to break out of nested loops. The loop@
label is assigned to the outer for
loop. Inside the nested loops, when the condition i == 3 && j == 3
is met, it prints "Breaking the loop" and executes break@loop
, which breaks out of the labeled loop. This behavior allows termination of both the inner and outer loops simultaneously when the condition is satisfied.