October 2018
Intermediate to advanced
370 pages
9h 15m
English
Break statements always break the nearest or parent loop where the break is placed, as we saw in previous example. But what if we want to stop the loop iterations altogether whenever a certain condition is met in a nested inner loop? To address such conditions, Kotlin provides a concept called a labeled for loop. This means that an alias name is assigned to a for loop to break it by using the break@nameOfTheLoop statement. Assign a name to the for loop, as described here, and call the loop by using the break statement. When the variables i and j are equal to 2, by using break@outLoop, the break statement would know which for loop to terminate:
fun main(args: Array<String>) { println("Labled For Loop") ...
Read now
Unlock full access