Kotlin break Statement

We have seen the usage of Flow of Control which allows us to quickly move apply conditions, now in this tutorial we will encounter the scenarios to be catered when the flow needs to be broken or redirected.

The way kotlin has given us some altered and improved for loop and a modern day switch which have reduced the developer’s effort, now let’s have look on topic without which no conditional flow can ever be truly complete, these are the break and continue.

We remember using break in majority of programming languages in switch cases where we can stop execution of a case by using the break statement, though Kotlin has moved a step forward and does not enforces the use of break in switch cases. A typical break is used in scenarios where we want to end the execution of nearest enclosing loop or a conditional statement (if, else –if or so), Let’s have a look for what it actually looks like.

Kotlin break Statement

Kotlin break statement

Image Source

fun main() {
    var sum = 0
    var count = 0
    do{
        sum = count+sum;
		
        if(count == 0){
            println("Break will be called as count is ZERO");
            break;
        }else{
            println("Execution without break as count is greater than 0.");
        }
        count++;
    }while (count <= 10);
    println("Sum of first 10 Natural Numbers is = $sum");
}

Output

Break will be called as count is ZERO
Sum of first 10 Natural Numbers is = 0

If we update the value of count to 1 and then execute, in that case the output would be like:

Execution without break as count is greater than 0.
Execution without break as count is greater than 0.
Execution without break as count is greater than 0.
Execution without break as count is greater than 0.
Execution without break as count is greater than 0.
Execution without break as count is greater than 0.
Execution without break as count is greater than 0.
Execution without break as count is greater than 0.
Execution without break as count is greater than 0.
Execution without break as count is greater than 0.
Sum of first 10 Natural Numbers is = 55

So, when break is used we simply end the execution at the current iteration but what about scenarios when we are want the iteration to skip something more than the current loop. When we want to terminate execution of outer loop or a particular loop, then we can use labeled break. Label are simple text which end with @ symbol.

For Example:

[email protected]

Kotlin labelled break statement

Image Source

Let’s take the example to understand the basic working of break condition, we have taken a scenario in which we are iterating over two loops each from 0 to 100 and a condition to break the flow of control when if the loop counter matches the input number. For sake of simplicity we have taken input number as hard coded number. We here aim to understand what should happen when the break is called with label and flow is redirected to the outer loop.

fun main(args: Array<String>) {
    var breakNum = 20
    
    [email protected] for (oLC in 1..100) {
    				for (iLC in 1..100) {
                        if (breakNum == iLC)
                        	[email protected];
                        else
                        	println("In Inner Loop : $iLC");
                    }
        			
        			println("In Outer Loop : $oLC");
    		}
}

Output

In Inner Loop : 1
In Inner Loop : 2
In Inner Loop : 3
In Inner Loop : 4
In Inner Loop : 5
In Inner Loop : 6
In Inner Loop : 7
In Inner Loop : 8
In Inner Loop : 9
In Inner Loop : 10
In Inner Loop : 11
In Inner Loop : 12
In Inner Loop : 13
In Inner Loop : 14
In Inner Loop : 15
In Inner Loop : 16
In Inner Loop : 17
In Inner Loop : 18
In Inner Loop : 19

So, we can use labels in case we want to one of the outer loop to stop execution in case of a different or unexpected value pops up, in those cases we can use the break condition with labels.

Break forms one of the most important part of the Jump statements for Kotlin, out of the three jumps statement which are return and continue. Let’s move ahead to look at the other two jump statements.

The details of these we will find in the next tutorial.

Leave a Comment