break, continue, and Labeled Returns
Control loop flow with break/continue and escape nested loops with labels.
Loop Flow Control
Kotlin supports break (exit the loop), continue (skip to next iteration), and labels for breaking out of nested loops.
break Basics
break exits the innermost enclosing loop immediately.
fun main() {
for (i in 1..10) {
if (i == 5) break
print("$i ")
}
println() // 1 2 3 4
}All lessons in this course
- for Loop Over Ranges and Collections
- while and do-while: When to Use Each
- forEach, repeat, and forEachIndexed
- break, continue, and Labeled Returns