Labeled Break in Kotlin With Example

In this post, we will see how to use labeled break in kotlin. In previous post, we have seen how to use unlabeled (Or Simple) break in kotlin. We have seen how to use break with for loop, while loop or do while loop in kotlin.

There is another type of break in kotlin. This is Labeled break in kotlin. As we know, unlabeled break in kotlin is used to terminate the nearest enclosing loop in the program. What if we want to terminate the outer loop? Or, what if we want to terminate a desired loop when certain conditions are satisfied in the program?
It can be achieved by using labeled break in kotlin. We can terminate any outer or desired loop using labeled form of break in kotlin.

Labeled break in kotlin is mostly used with if statement. An identifier followed by the @ sign is called label. For example, @first, @second, @outer, @inner etc. are some valid labels. In kotlin, you may use label with any expression. You just need to write label in front of it.

Now, we will learn how to use labeled break in for loop, while loop and do while loop.

Syntax to Use labeled break in for Loop in Kotlin

Syntax to use labeled break in for loop in kotlin –

Tutorialwing - Labeled break in For loop In Kotlin

Tutorialwing – Labeled break in For loop In Kotlin

Examples to Show labeled break in for Loop in Kotlin

1. Examples to demonstrates the use of labeled break in kotlin in for loop
fun main(args: Array<String>) {
    first@ for (i in 1..10) {

        second@ for (j in 1..5) {
            println("i = $i; j = $j")

            if (i == 2)
                break@first
        }
    }
}

When you run the program, you will get below output:
i = 1; j = 1
i = 1; j = 2
i = 1; j = 3
i = 1; j = 4
i = 1; j = 5
i = 2; j = 1

We are checking whether i is equal to 2 or not in each iteration of inner for loop. If yes, code inside if expression block will be executed. We have break@first in if expression block. It means terminate the first loop. Notice that first is outer loop.

2. Another example to show use of labeled break in kotlin
fun main(args: Array<String>) {
    first@ for (i in 1..5) {
        second@ for (j in 1..3) {
            println("i = $i; j = $j")

            if (i == 3)
                break@second
        }
    }
}

When you run the program, you will get below output
i = 1; j = 1
i = 1; j = 2
i = 1; j = 3
i = 2; j = 1
i = 2; j = 2
i = 2; j = 3
i = 3; j = 1
i = 4; j = 1
i = 4; j = 2
i = 4; j = 3
i = 5; j = 1
i = 5; j = 2
i = 5; j = 3

Notice that we are checking whether i is equal to 3 or not in each iteration of inner for loop. If yes, code inside if expression block will be executed. We have break@second in if expression block. It means terminate the second loop. You can also simply mention break because second labeled loop is enclosing loop of break statement.

Now, We have some questions for you

1. Guess the output of below program.
fun main(args: Array<String>) {
    outer@ for (i in 1..3) {
        inner@ for (j in 1..6) {
            println("$j")

            if (j == 6)
                break@outer
        }
    }
}
2. What is output of the program
fun main(args: Array<String>) {
     first@ for (i in 5 downTo 1 step 2) {
        second@ for (j in 1..2) {
            println("$j")

            if (i == 2)
                break@second
        }
    }
}



Syntax to Use labeled break in while Loop in Kotlin

Syntax to use labeled break in while loop in kotlin –

Tutorialwing - Labeled break in while loop in Kotlin

Tutorialwing – Labeled break in while loop in Kotlin

Examples to Show labeled break in while Loop in Kotlin

Let’s see some examples that demonstrates the use of break in while loop in kotlin.

1. An example to demonstrate the use of labeled break in kotlin

fun main(args: Array<String>) {
    var i = 5
    first@ while (i > 0) {
        var j = 5
        second@ while (j > 0) {
            if (i == 3)
                break@second
            println("i = $i; j = $j")
            j--
        }
        i--
    }
}

When you run the program, you will get below output
i = 5; j = 5
i = 5; j = 4
i = 5; j = 3
i = 5; j = 2
i = 5; j = 1
i = 4; j = 5
i = 4; j = 4
i = 4; j = 3
i = 4; j = 2
i = 4; j = 1
i = 2; j = 5
i = 2; j = 4
i = 2; j = 3
i = 2; j = 2
i = 2; j = 1
i = 1; j = 5
i = 1; j = 4
i = 1; j = 3
i = 1; j = 2
i = 1; j = 1

This is an example to demonstrate the use of break in while loop in kotlin. We are checking if i is equal to 3 or not. If yes, we are breaking the second loop. Note that second is enclosing loop.So, we may do the same work by simply using break instead of labelled break.

2. Guess the output of below program.
fun main(args: Array<String>) {
    var i = 5
    first@ while (i > 0) {
        var j = 5
        second@ while (j > 0) {
            if (i == 3)
                break@second
            println("i = $i; j = $j")
            j--
        }
        i--
    }
}
3. Guess the output of below program.
fun main(args: Array<String>) {
    var i = 5
    var j = 5
    outer@ while (i > 0) {
        inner@ while (j > 0) {
            if (i == 3)
                break@outer
            println("i = $i; j = $j")
            j--
        }
        i--
    }
}

Syntax to Use labeled break in do while Loop in Kotlin

Syntax to use labeled break in do while loop in kotlin –

Tutorialwing - Labeled break in do while loop in kotlin

Tutorialwing – Labeled break in do while loop in kotlin

Examples to Show labeled break in do while Loop in Kotlin

Let’s see some examples that demonstrates the use of break in do while loop in kotlin.

1. An example to demonstrate the use of labeled break in kotlin.

fun main(args: Array<String>) {
    var i = 5
    var j = 5
    outer@ do {
        inner@ do {
            if (i == 3)
                break@outer
            println("i = $i; j = $j")
            j--
        } while (j > 0)
        i--
    } while (i > 0)
}

When you run the program, you will get below output
i = 5; j = 5
i = 5; j = 4
i = 5; j = 3
i = 5; j = 2
i = 5; j = 1
i = 4; j = 0

This is an example to demonstrate the use of labelled break in do while loop in kotlin. Here, we are checking if i is equal to 3 or not. If yes, we are terminating the outer do while loop.

2. Guess the output of below program
fun main(args: Array<String>) {
     var i = 5
    outer@ do {
        var j = 5
        inner@ do {
            if (i == 3)
                break@outer
            println("i = $i; j = $j")
            j--
        } while (j > 0)
        i--
    } while (i > 0)
}



3. Guess the output of below program
fun main(args: Array<String>) {
    var i = 5
    outer@ do {
        var j = 5
        inner@ do {
            if (i == 3)
                break@inner
            println("i = $i; j = $j")
            j--
        } while (j > 0)
        i--
    } while (i > 0)
}

Conclusion

In this post, we have seen how to use labeled break in Kotlin. we have also learnt how to use labeled break in for loop, while loop or do while loop in kotlin.

Leave a Reply