[Kotlin] Named loop

It is possible to give loop a name, it is useful when dealing with nested loop:

fun main() {
    outer@ for (i in 1..10) {
        for (j in 1..10) {
            if (i - j == 5) {
                break@outer
            }
            println("$i - $j")
        }
    }
}

Normally "break", only skip the inner for loop, when we give a name for the outer loop, and say break@outer, so it will skip outer for loop instead of inner one

posted @ 2020-10-11 20:07  Zhentiw  阅读(68)  评论(0编辑  收藏  举报