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:
As we all know, Unlabelled break is used to terminate to the closest enclosing loop when certain condition is satisfied.
But labelled 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 labelled break expression in while, do-while and for loop.
Use of labelled break in while loop –
Labelled 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 labelled break in while loop-
outer@ while(condition) {
// code
inner@ while(condition) {
// code
if(break condition) {
break @outer
}
}
}
Kotlin program of using labelled break in while loop –
Kotlin
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--
}
}
|
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 labelled break in do-while loop –
In do-while loop also the labelled 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 labelled break in do-while loop –
outer@ do {
// code
inner@ do {
// code
if(break condition) {
break@outer
}
} while(condition)
} while(condition)
Kotlin program of using labelled break in do-while loop-
Kotlin
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)
}
|
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 labelled break in for loop –
In for loop also we can use the labelled break to terminate the desired loop for certain condition. We have labelled the outer for loop as outer@ and inner for loop as inner@. In for loop, iteration is to be done through iterator.
Syntax of labelled 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 labelled break in for-loop-
Kotlin
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")
}
}
}
|
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
Similar Reads
Kotlin labelled 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 express
4 min read
Kotlin Unlabelled break
When we are working with loops and want to stop the execution of loop immediately if a certain condition is satisfied, in this case, we can use either break or return expression to exit from the loop. In this article, we will discuss learn how to use break expression to exit a loop. When break expre
4 min read
Kotlin labelled continue
In this article, we will learn how to use continue in Kotlin. While working with a loop in the programming, sometimes, it is desirable to skip the current iteration of the loop. In that case, we can use the continue statement in the program. Basically, continue is used to repeat the loop for a speci
4 min read
Kotlin unlabelled continue
In this article, we will learn how to use continue in kotlin. While working with loop in the programming, sometimes, it is desirable to skip the current iteration of the loop. In that case, we can use continue statement in the program. Basically, continue is used to repeat the loop for a specific co
3 min read
Kotlin for loop
In Kotlin, for loop is equivalent to foreach loop of other languages like C#. Here for loop is used to traverse through any data structure which provides an iterator. It is used very differently then the for loop of other programming languages like Java or C. The syntax of for loop in Kotlin: for(it
4 min read
Returns, Jumps and Labels in Kotlin
Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and a new language for the JVM. Kotlin is an object-oriented language, and a âbetter lan
3 min read
Kotlin mutableMapOf()
Kotlin MutableMap is an interface of collection frameworks which contains objects in the form of keys and values. It allows the user to efficiently retrieve the values corresponding to each key. The key and values can be of the different pairs like <Int, String>, < Char, String>, etc.For
4 min read
"lateinit" Variable in Kotlin
In Kotlin, there are some tokens that cannot be used as identifiers for naming variables, etc. Such tokens are known as keywords. In simple words, keywords are reserved and have special meaning to the compiler, hence they can't be used as identifiers. For example, asbreakclasscontinuelateinit "latei
3 min read
Kotlin Ranges
In Kotlin, the range is a collection of finite values which is defined by endpoints. The range in Kotlin consists of a start, a stop, and the step. The start and stop are inclusive in the Range and the value of step is by default 1. The range is used with comparable types. There are three ways for c
4 min read
Kotlin while loop
In programming, loop is used to execute a specific block of code repeatedly until certain condition is met. If you have to print counting from 1 to 100 then you have to write the print statement 100 times. But with help of loop you can save time and you need to write only two lines. While loop - It
2 min read