Returns, Jumps and Labels in Kotlin
Last Updated :
10 Nov, 2021
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 language” than Java, but still be fully interoperable with Java code. As the Kotlin says: Kotlin has three structural jump expressions as follow:
- return
- break
- continue
These can be a part of the large expression as Kotlin say. So, let's start with "return".
1. return
It's a statement that generally we use in functions during declaration for returning the values after execution of a function. By default it returns from the nearest enclosing function or anonymous function. Let's take an example,
Example:
Kotlin
fun add(a:Int,b:Int) : Int {
// ans having final value after execution
val ans = a + b
// here we have returned it
return ans
}
fun main(args: Array<String>) {
val first: Int = 10
val second: Int = 20
// called function and
// Collected the returned value in sum
val sum = add(first,second)
println("The sum is: $sum")
}
Another use of return
Kotlin
fun GfG() {
listOf(1, 2, 3, 4, 5).forEach {
// non-local return directly
// to the caller of GfG()
if (it == 3) return
print(it)
}
println("this point is unreachable")
}
So, that's how the return statement work.
2. Jumps & labels
2.1. break
A break statement is used to terminate the flow of a loop. but only terminates the nearest enclosing loop i.e. if you are having two nested for-loops and the break statement is present in the inner for-loop then the inner for-loop will be terminated first and after that, if another break is added then the outer for-loop will also be terminated.
Example:
Kotlin
fun main(args : Array<String>){
for(ch in 'A'..'C'){ // Outer loop
for (n in 1..4){ // Inner loop
println("processing...")
if(n == 2) // it will terminate Inner loop
break
}
if(ch == B)
break // but it will terminate Outer loop
}
}
We can also optimize the above code or reduce the line of code as well, using labels.
2.2. labels
Any expression in Kotlin may be marked with a label. Labels have the form of an identifier followed by the @ sign, such as name@ or xyz@. To label an expression, just add a label in front of it.
Example:
Kotlin
fun main(args : Array<String>){
Outerloop@ for(ch in 'A'..'C'){ // Outer loop
for (n in 1..4){ // Inner loop
println("processing...")
if(n == 2) // it will terminate Outerloop directly
break@Outerloop
}
// here we don't need it
/*
if(ch==B)
break
*/
}
}
2.3. continue
It is the same as the break statement but the only difference is, the break statement terminates the whole iteration of a loop whereas continuing skips the current iteration and we can use labels here as well.
Example:
Kotlin
fun main(args: Array<String>) {
// outerloop is label name
outerloop@ for (i in 1..5) {
for (j in 1..4) {
if (i == 3 || j == 2)
// here we have used that
continue@outerloop
println("Happy Diwali!")
}
}
}
That's all about return, jump, and labels.
Similar Reads
Kotlin labelled continue In this article, we will learn how to use continue in Kotlin. While working with a loop in 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. continue is used to repeat the loop for a specific condition.
4 min read
Multiple Return Values in Kotlin Kotlin is a statically typed, general-purpose programming language created by JetBrains, the company that also built world-famous IDEs like IntelliJ IDEA, PhpStorm, and AppCode. Kotlin was first introduced by JetBrains in 2011 and is designed to run on the Java Virtual Machine (JVM). Kotlin is an ob
3 min read
Kotlin unlabelled continue In this article, we will learn how to use continue in Kotlin. While working with loops in 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. continue is used to repeat the loop for a specific condition. I
3 min read
Kotlin Setters and Getters In Kotlin, properties are a core feature of the language, providing a clean and concise way to encapsulate fields while maintaining control over how values are accessed or modified. Each property can have getters and setters, which are automatically generated but can be customized as needed.Kotlin P
4 min read
Kotlin Class and Objects In Kotlin, classes and objects are used to represent objects in the real world. A class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or fields), and implementations of behavior (member functions or methods). An object is an i
4 min read
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 expressi
4 min read