Kotlin labeled break

While working with loops say you want to stop the execution of loop immediately if a certain condition is satisfied. In this case you can use either break or return expression to exit from the loop.
In this article, we are going to learn how to use break expression to exit a loop. When break expression encounters in a program it terminates to nearest enclosing loop.

There are two types of break expression in Kotlin:



  1. Labeled break
  2. Unlabeled break

As we all know, Unlabeled break is used to terminate to the closest enclosing loop when certain condition is satisfied.
But labeled break is used to terminate to a desired loop when certain condition is satisfied. It can be done with the help of labels. An identifier followed by @ sign is called label e.g.- inner@, outer@, first@, second@ etc. You can use label with any expression and it should be written in front of it.

We are going to learn how to use labeled break expression in while, do-while and for loop.

Use of labeled break in while loop –

Labeled break is used to exit to the desired block when it satisfy a specific condition without checking the condition in while loop. Then, transfers the control to following statement of while block.
If you mark the outer loop using the label outer@ then you can easily break the outer loop using break@outer in the break condition block.

Syntax of labeled break in while loop-

outer@ while(condition) {
      // code
      inner@ while(condition) {
            // code
            if(break condition) {
               break @outer
            } 
      }
}

Kotlin program of using labeled break in while loop –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var num1 = 4
    outer@ while (num1 > 0) {
        var num2 = 4
        inner@ while (num2 > 0) {
            if (num1==2)
                break@outer
            println("num1 = $num1, num2 = $num2")
            num2--
        }
        num1--
    }
}

chevron_right


Output:

num1 = 4, num2 = 4
num1 = 4, num2 = 3
num1 = 4, num2 = 2
num1 = 4, num2 = 1
num1 = 3, num2 = 4
num1 = 3, num2 = 3
num1 = 3, num2 = 2
num1 = 3, num2 = 1

When (num1 == 2) expression is evaluated to be true, the break@outer is executed which terminates the desired loop marked with outer@.

Use of labeled break in do-while loop –

In do-while loop also the labeled break is executed to terminate the desired loop. Here we have used outer@ for the outer do-while and inner@ for the inner do-while loop.

Syntax of labeled break in do-while loop –

outer@ do {
       // code
       inner@ do {
           // code
           if(break condition) {
              break@outer
           }
       } while(condition)
} while(condition)

Kotlin program of using labeled break in do-while loop-



filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var num1 = 4
    outer@ do {
        var num2 = 4
  
        inner@ do {
            if (num1 == 2)
                break@outer
            println("num1 = $num1; num2 = $num2")
            num2--
        } while (num2 > 0)
        num1--
    } while (num1 > 0)
}

chevron_right


Output:

num1 = 4; num2 = 4
num1 = 4; num2 = 3
num1 = 4; num2 = 2
num1 = 4; num2 = 1
num1 = 3; num2 = 4
num1 = 3; num2 = 3
num1 = 3; num2 = 2
num1 = 3; num2 = 1

Here, we print the same output as while loop. When (num1 == 2) expression is evaluated to be true, the break@outer is executed which terminates the desired loop marked with outer@.

Use of labeled break in for loop –

In for loop also we can use the labeled break to terminate the desired loop for certain condition. We have labeled the outer for loop as outer@ and inner for loop as inner@. In for loop, iteration is to be done through iterator.

Syntax of labeled break in for loop –

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

Kotlin program of using labeled break in for-loop-

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    outer@ for (num1 in 4 downTo 1) {
  
        inner@ for (num2 in 4 downTo 1) {
            if (num1 == 2)
                break@outer
            println("num1 = $num1; num2 = $num2")
        }
    }
}

chevron_right


Output:

num1 = 4; num2 = 4
num1 = 4; num2 = 3
num1 = 4; num2 = 2
num1 = 4; num2 = 1
num1 = 3; num2 = 4
num1 = 3; num2 = 3
num1 = 3; num2 = 2
num1 = 3; num2 = 1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.