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 expressions in Kotlin:
As we all know, Unlabelled break is used to terminate the closest enclosing loop when a certain condition is satisfied.
But labelled break is used to terminate a loop when a certain condition is satisfied. It can be done with the help of labels. An identifier followed by the @ sign is called a label, e.g., inner@, outer@, first@, second@, etc. You can use a label with any expression, and it should be written in front of it.
We are going to learn how to use labelled break expressions in while, do-while, and for loops.
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 using labelled break in a 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 using labelled break in a 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 a 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 using labelled break in a 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 Tutorial This Kotlin tutorial is designed for beginners as well as professional, which covers basic and advanced concepts of Kotlin programming language. In this Kotlin tutorial, you'll learn various important Kotlin topics, including data types, control flow, functions, object-oriented programming, collecti
4 min read
Kotlin Android Tutorial Kotlin is a cross-platform programming language that may be used as an alternative to Java for Android App Development. Kotlin is an easy language so that you can create powerful applications immediately. Kotlin is much simpler for beginners to try as compared to Java, and this Kotlin Android Tutori
6 min read
Retrofit with Kotlin Coroutine in Android Retrofit is a type-safe http client which is used to retrieve, update and delete the data from web services. Nowadays retrofit library is popular among the developers to use the API key. The Kotlin team defines coroutines as âlightweight threadsâ. They are sort of tasks that the actual threads can e
3 min read
Introduction to Kotlin Kotlin is a statically typed, general-purpose programming language developed by JetBrains, which has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 as a new language for the JVM. Kotlin is an object-oriented language, and a better lang
4 min read
Kotlin Data Types The most fundamental data type in Kotlin is the Primitive data type and all others are reference types like array and string. Java needs to use wrappers (java.lang.Integer) for primitive data types to behave like objects but Kotlin already has all data types as objects.There are different data types
3 min read
Kotlin when expression In Kotlin, when replaces the switch operator of other languages like Java. A certain block of code needs to be executed when some condition is fulfilled. The argument of when expression compares with all the branches one by one until some match is found. After the first match is found, it reaches to
6 min read
Android RecyclerView in Kotlin In this article, you will know how to implement RecyclerView in Android using Kotlin . Before moving further let us know about RecyclerView. A RecyclerView is an advanced version of ListView with improved performance. When you have a long list of items to show you can use RecyclerView. It has the ab
4 min read
Kotlin Constructor A constructor is a special member function that is automatically called when an object of a class is created. Its main purpose is to initialize properties or perform setup operations. In Kotlin, constructors are concise, expressive, and provide significant flexibility with features like default para
6 min read
ProgressBar in Android Progress Bar are used as loading indicators in android applications. These are generally used when the application is loading the data from the server or database. There are different types of progress bars used within the android application as loading indicators. In this article, we will take a lo
3 min read
Kotlin Array An Array is one of the most fundamental data structures in practically all programming languages. The idea behind an array is to store multiple items of the same data type, such as an integer or string, under a single variable name. Arrays are used to organize data in programming so that a related s
6 min read