August 2017
Intermediate to advanced
440 pages
10h
English
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 ...
Read now
Unlock full access