Kotlin break Statement Tutorial

In this section, we will learn what the break statement is and how to use it in Kotlin.

Note: we’re assuming you’re already familiar with loops like the for and while loops.

break Statement in Kotlin

The break statement is a way of breaking the iteration of a loop before it finishes its iteration normally.

Note that after calling the break statement in a loop, your program will stop that loop (terminate it immediately) and continue to run the instructions after the loop.

Kotlin break Statement Syntax:

break

The break statement doesn’t take any value. Simply call this statement in a loop and it will terminate that loop.

Example: for loop break statement

fun main(){

    for (num in 10 downTo 0 ){
        println("The number is: ${num}")
        if (num == 5){
            break
        }
    }
    println("This message is coming after the for loop")
}

Output:

The number is: 10

The number is: 9

The number is: 8

The number is: 7

The number is: 6

The number is: 5

This message is coming after the for loop

As you can see, the for loop here is supposed to iterate through the sequence from 10 to 0. But then right when the value of the num variable reached to 5, a call to the break statement is triggered and caused the loop to break and the instructions after the loop ran afterward.

Example: while loop break statement

fun main(){

    var num = 0 

    while (num <10){
        println("The value of the num variable is: ${num}")
        if (num == 4){
            break
        }
        num++
    }
    println("This message is coming after the for loop")
}

Output:

The value of the num variable is: 0

The value of the num variable is: 1

The value of the num variable is: 2

The value of the num variable is: 3

The value of the num variable is: 4

This message is coming after the for loop

Kotlin break Statement in Nested Loop

The break statement can be used within the body of a loop, no-matter if that loop is a nested loop or not. But be aware that when calling the break statement in the body of a nested loop, only the inner loop will be ended and not the parent loop!

Basically, the break statement only affects its enclosing loop.

Example: nested loop and break statement

fun main(){

    for (num in 0..3){
        println("*** The value of the num variable is: ${num}***")
        for (num2 in 0..4){
            println("The value of the num2 variable is:${num2} ")
            if (num2 ==3){
                break
            }
        }
    }
    println("This message is coming after the for loop")
}

Output:

*** The value of the num variable is: 0***

The value of the num2 variable is:0

The value of the num2 variable is:1

The value of the num2 variable is:2

The value of the num2 variable is:3

*** The value of the num variable is: 1***

The value of the num2 variable is:0

The value of the num2 variable is:1

The value of the num2 variable is:2

The value of the num2 variable is:3

*** The value of the num variable is: 2***

The value of the num2 variable is:0

The value of the num2 variable is:1

The value of the num2 variable is:2

The value of the num2 variable is:3

*** The value of the num variable is: 3***

The value of the num2 variable is:0

The value of the num2 variable is:1

The value of the num2 variable is:2

The value of the num2 variable is:3

This message is coming after the for loop

Difference between break and continue (Break vs Continue)

Other than the break statement, there’s another statement called continue which we will talk about it in the Kotlin continue statement section.

The main difference between the two statements is that:

  • The break statement stops the entire loop and makes a program to continue running the instructions that come after the loop.
  • On the other hand, the continue statement only stops the current iteration of a loop and makes a program to continue running the next iteration of the same loop. (More about this in the next section)
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies