Menu
Kotlin Tutorial
Menu
Kotlin Classes
Menu
Kotlin Break and Continue
Kotlin Break
The break statement terminates the nearest enclosing loop.
Example:
var number = 0
while (number < 5) {
println(number)
number ++
if (number == 2) {
break
}
}
The code above outputs
0
1
In the example above the loop terminates when the number becomes equal to 2.
Kotlin Continue
The continue statement breaks the loop in the middle and ignores the value specified in condition; and continues with the next iteration in the loop.
Example
var number = 0
while (number < 5) {
if (number == 2) {
number++
continue
}
println(number)
number++
}
The code above outputs:
0
1
3
4
In the example above, 2 is skipped and the other numbers are printed in the output.