Site icon C1CTech

Kotlin break

In this tutorial, you will learn to use break to terminate a loop. Also, you will learn about break labels.

Kotlin break example

The break is a loop control statement which is used to terminate the loop.

Basically break statements are used in the situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop based on some condition.
As soon as the break statement is encountered, it terminates the nearest enclosing loop and control returns from the loop immediately to the first statement after the loop.

Syntax

Syntax of break in for loop:

for (...) {
    if (test-expression) {
        break
    }
}

If test-expression is evaluated to true, break is executed which terminates the for loop.

Example

fun main(args: Array<String>) {

    for (n in 1..10) {
        if (n == 5) {
            break
        }
        println("$n")
    }
}

Output

1
2
3
4

Example explained:

In the above example,when n= 5

Kotlin break example in nested loop

When break is used in the nested loop (loop inside another loop), it terminates the inner loop when it is encountered.

Example

fun main(args: Array<String>) {

    for (i in 1..3) {
        for (j in 1..2) {
            println("i=$i and j=$j")
            if (i == 2)
                break
        }
    }
}

Output

i=1 and j=1
i=1 and j=2
i=2 and j=1
i=3 and j=1
i=3 and j=2

Example explained:

In the above example, when i= 2:

Kotlin Labeled break

In Kotlin, labeled break is used to terminate the desired loop when certain condition is satisfied. It can be done with the help of labels.

Label starts with an identifier which is followed by @. e.g.- inner@, outer@ etc.

Syntax

Syntax of labeled break in for loop:

outer@ for(iteration through iterator) {
    // code
    inner@ for(iteration through iterator) {
    // code
    if(break condition) {
        break@outer
    }
}
}

Here, outer@ is a label marked at the outer for loop.

Now, by using break with a label (break@outer in this case), breaks the specific loop(outer loop), when the break condition is evaluated to true.

Example

fun main(args: Array<String>) {

    outer@ for (i in 1..3) {
        inner@ for (j in 1..2) {
            println("i=$i and j=$j")
            if (i == 2)
                break@outer
        }
        println("Outside inner loop")
    }
    println("Outside outer loop")

}

Output

i=1 and j=1
i=1 and j=2
Outside inner loop
i=2 and j=1
Outside outer loop

Example explained:

In the above example, when i= 2:

In the below program, break terminates the loop marked with label inner@.

Example

fun main(args: Array<String>) {

    outer@ for (i in 1..3) {
        inner@ for (j in 1..2) {
            println("i=$i and j=$j")
            if (i == 2)
                break@inner
        }
        println("Outside inner loop")
    }
    println("Outside outer loop")

}

Output

i=1 and j=1
i=1 and j=2
Outside inner loop
i=2 and j=1
Outside inner loop
i=3 and j=1
i=3 and j=2
Outside inner loop
Outside outer loop

Example explained:

In the above example, when i= 2:

Note: Since, break is used to terminate the innermost loop, in the above program, it is not necessary to use labeled break in this case.

 

 

Exit mobile version