2
2
.
.
3
3
.
.
5
5
S
S
t
t
a
a
t
t
e
e
m
m
e
e
n
n
t
t
s
s
-
-
J
J
u
u
m
m
p
p
I
I
n
n
f
f
o
o
[
[
R
R
]
]
Jump Statements are used to skip execution of subsequent code
break completely exits current or labelled Loop Statement
continue skips execution of remaining current iteration of current or labelled Loop (continues with next iteration)
return returns from the function by skipping execution of the remaining function code
b
b
r
r
e
e
a
a
k
k
break statement is used to completely exit Loop Statements and Switch Blocks.
This means that remaining of the current iteration and all subsequent iterations of the Loop Statement are skipped.
Exit loop
//EXIT NEAREST LOOP.
for(i in 1..5) {
if(i==3) { break } //Exit nearest Loop.
print(i) //1, 2
}
//EXIT LABELED LOOP.
myloop@
for(i in 1..5) {
for(j in 10..15) {
if(j==13) { break@myloop } //Exit labeled Loop.
println(j) //10, 11, 12
}
}
c
c
o
o
n
n
t
t
i
i
n
n
u
u
e
e
continue statement is used to skip only the remaining of the current iteration of the Loop Statement.
But subsequent iterations of the Loop Statement are to be executed normally.
Continue with next iteration
//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.
myloop@
for(i in 1..3) {
for(j in 10..15) {
if(j==13) { continue@myloop } //Continue with labeled Loop.
println(j) //(10, 11, 12) (10, 11, 12) (10, 11, 12)
}
}
r
r
e
e
t
t
u
u
r
r
n
n
return statement is used to exit from the function. Code execution continues from where the function was called.
It can also be used to skip execution of the remaining function code.
return statement must be followed by the value if Function should return value.
Unqualified return is used to return from named or anonymous functions. return
Qualified return is used to return from lambda expression. return@someLabel
Return Value
//DECLARE FUNCTION.
fun test() : String {
return("Hello from test.") //Return from the nearest enclosing function.
print("Never executed") //Skipped line.
}
//MAIN FUNCTION.
fun main() {
var result = test() //Store function return value into variable result.
print(result)
}