Break
//BREAK FROM NEAREST LOOP.
for(i in 1..5) {
if(i==3) { break } //Break from nearest Loop.
print(i) //1, 2
}
//BREAK FROM LABELED LOOP.
beginLoop@
for(i in 1..5) {
for(j in 10..15) {
if(j==13) { break@beginLoop } //Break from labeled Loop.
println(j) //10, 11, 12
}
}
Continue
//CONTINUE WITH NEAREST LOOP.
for(i in 1..5) {
if(i==3) { continue } //Continue with nearest Loop.
print(i) //1, 2, 4, 5
}
//CONTINUE WITH LABELED LOOP.
beginLoop@
for(i in 1..3) {
for(j in 10..15) {
if(j==13) { continue@beginLoop } //Continue with labeled Loop.
println(j) //(10, 11, 12) (10, 11, 12) (10, 11, 12) (10, 11, 12) (10, 11, 12)
}
}
Return
//DECLARE FUNCTION.
fun test() : String {
return("Hello from test.") //Return from the nearest enclosing function.
print("Never executed") //Skipped line.
}
//CALL FUNCTION.
var result = test() //Store function return value into variable result.
print(result)