All loops in Kotlin support classic break and continue statements. The continue statement proceeds to the next iteration of that loop, while break stops the execution of the most inner enclosing loop:
val range = 1..6
for(i in range) {
print("$i ")
}
// prints: 1 2 3 4 5 6
Now let's add a condition and break the iteration when this condition is true:
val range = 1..6
for(i in range) {
print("$i ")
if (i == 3)
break
}
// prints: 1 2 3
The break and continue statements are especially useful when dealing with nested loops. They may simplify our control flow and significantly decrease the amount of work performed to save priceless Android resources. Let's perform a nested iteration and break the outer loop:
val intRange = 1..6
val charRange = 'A'..'B'
for(value in intRange) {
if(value == 3)
break
println("Outer loop: $value ")
for (char in charRange) {
println("\tInner loop: $char ")
}
}
// prints
Outer loop: 1
Inner loop: A
Inner loop: B
Outer loop: 2
Inner loop: A
Inner loop: B
We used a break statement to terminate the outer loop at the beginning of the third iteration, so the nested loop was also terminated. Notice the usage of the \t escaped sequence, which adds indents on the console. We can also utilize the continue statement to skip the current iteration of the loop:
val intRange = 1..5
for(value in intRange) {
if(value == 3)
continue
println("Outer loop: $value ")
for (char in charRange) {
println("\tInner loop: $char ")
}
}
// prints
Outer loop: 1
Inner loop: A
Inner loop: B
Outer loop: 2
Inner loop: A
Inner loop: B
Outer loop: 4
Inner loop: A
Inner loop: B
Outer loop: 5
Inner loop: A
Inner loop: B
We skip the iteration of the outer loop when the current value equals 3.
Both continue and break statements perform corresponding operations on the enclosing loop. There are, however, times when we want to terminate or skip the iteration of one loop from within another; for example, to terminate an outer loop iteration from within an inner loop:
for(value in intRange) {
for (char in charRange) {
// How can we break outer loop here?
}
}
Fortunately, both continue and break statements have two forms--labeled and unlabeled. We already saw unlabeled; now we will need labeled to solve our problem. Here is an example of how a labeled break might be used:
val charRange = 'A'..'B'
val intRange = 1..6
outer@ for(value in intRange) {
println("Outer loop: $value ")
for (char in charRange) {
if(char == 'B')
break@outer
println("\tInner loop: $char ")
}
}
// prints
Outer loop: 1
Inner loop: A
The @outer is the label name. By convention, the label name always starts with @ followed by the label name. The label is placed before the loop. Labeling the loop allows us to use a qualified break (break@outer), which is a way to stop execution of a loop that is referenced by this label. The preceding qualified break (a break with a label) jumps to the execution point right after the loop marked with that label.
Placing the return statement will break all the loops and return from enclosing an anonymous or named function:
fun doSth() {
val charRange = 'A'..'B'
val intRange = 1..6
for(value in intRange) {
println("Outer loop: $value ")
for (char in charRange) {
println("\tInner loop: $char ")
return
}
}
}
//usage
println("Before method call")
doSth()
println("After method call")
// prints Outer loop: 1 Inner loop: A