Kotlin break: In this tutorial, you will learn to use break to terminate a loop. Additionally, you will likewise learn about break labels.

Assume you are working with loops. It is here and there describe to terminate the loop promptly without checking the test articulation.

In such a case, the break is used. It ends the closest encasing loop when experienced (without checking the test articulation). This is like how the break statement works in Java.


How break works?

break is not a new concept. It is widely used in Java. In this tutorial, we will learn about Kotlin break expression.

break watchword is fundamentally used to terminate the loop. While writing code, so often you need to terminate the loop after a specific condition is coordinated. In such cases break is useful.

It is quite often used with if..else construct. For instance,

for (...) {
    if (testExpression) {
        break
    }
}

In the event that testExpression is assessed to true, break is executed which ends the for loop.


Example: Kotlin break

fun main(args: Array<String>) {

    for (i in 1..10) {
        if (i == 5) {
            break
        }
        println(i)
    }
}

At the point when you run the program, the output will be:

1
2
3
4

At the point when the value of I is equivalent to 5, expression I == 5 inside if is assessed to true, and break is executed. This terminates the for loop.


Example: Calculate Sum Until User enters 0

The program underneath calculates the sum of numbers entered by the user until user enters 0.

Visit Kotlin Basic Input Output to learn more on how to take input from the user.

fun main(args: Array<String>) {

    var sum = 0
    var number: Int

    while (true) {
        print("Enter a number: ")
        number = readLine()!!.toInt()

        if (number == 0)
            break

        sum += number
    }

    print("sum = $sum")
}

At the point when you run the program, the output will be:

Enter a number: 4
Enter a number: 12
Enter a number: 6
Enter a number: -9
Enter a number: 0
sum = 13

In the above program, the test articulation of the while loop is in every case true.

Here, the while loop runs until user enters 0. At the point when user inputs 0, break is executed which terminates the while loop.


Kotlin Labeled break

What you have discovered till now is the unlabeled type of break, which terminates the closest enclosing loop. There is another way break can be used (labeled form) to terminates the desired loop (can be external loop).


How labeled break works?

Name in Kotlin begins with an identifier which is followed by @.

Here, [email protected] is a label marked at the external while loop. Presently, by using break with a label ([email protected] for this situation), you can break the particular loop.

Here’s an example:

fun main(args: Array<String>) {

    [email protected] for (i in 1..4) {

        [email protected] for (j in 1..2) {
            println("i = $i; j = $j")

            if (i == 2)
                [email protected]
        }
    }
}

At the point when you run the program, the output will be:

i = 1; j = 1
i = 1; j = 2
i = 2; j = 1

Here, when I == 2 expression is assessed to true, [email protected] is executed which terminates the loop marked with label [email protected]


Here’s a little variety of the above program.

In the program beneath, break terminates the loop set apart with label @second.

fun main(args: Array<String>) {

    [email protected] for (i in 1..4) {

        [email protected] for (j in 1..2) {
            println("i = $i; j = $j")

            if (i == 2)
                [email protected]
        }
    }
}

At the point when you run the program, the output will be:

i = 1; j = 1
i = 1; j = 2
i = 2; j = 1
i = 3; j = 1
i = 3; j = 2
i = 4; j = 1
i = 4; j = 2

Note: Since, break is used to terminate the innermost loop in this program, it isn’t important to use labeled break for this situation.


There are 3 structural jump expressions in Kotlin: break, continue and return. To learn about continue and return expression, visit:

Kotlin continue
Kotlin function


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.


0 Comments

Leave a Reply

Avatar placeholder