Kotlin Break Continue Statement
10. Kotlin Break Continue
10.1 Kotlin Break Statement
10.2 Kotlin Continue Statement
10.3 Kotlin Return Statement
Kotlin Break Statement
Kotlin provides the break statement to implement the middle-existing control logic. The break statement causes the immediate exit from the body of the loop.
Kotlin Continue Statement
The continue statement is similar to the break statement. When continue statement is executed, it stops the current iteration of the loop and continue with the next iteration of the loop.
The statement in kotlin is marked with a label. The label has the form of an identifier followed by the @ sign.
Example of break continue
fun main(args : Array<String>) {
loop@ for (n in 1..100) {
println("$n")
if (n == 10)
break@loop
}
val l2 = listOf(10, 11, 12, 13)
loop@ for (m in l2) {
if(m == 11) {
println("This is if block")
break@loop
} else {
println("This is else block")
continue@loop
}
}
}
Output
1
2
3
4
5
6
7
8
9
10
This is else block
This is if block
Kotlin Return Statement
The return expression returns some values from the nearest enclosing function.
Example
fun main(args : Array<String>) {
val c = city()
println("City = $c")
}
fun city() : String {
return "Pune"
}
Output
City = Pune