Table of contents
1.
Introduction
2.
Return
3.
Break
4.
Continue
5.
Labels
5.1.
Labeled break statement
5.2.
Labeled continue statement
6.
FAQs
7.
Key Takeaways  
Last Updated: Mar 27, 2024

Kotlin Return and Jump

Author Pradeep Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will discuss jump expressions. Jump expressions are used to control the flow of execution of code. We will discuss how and where to use a particular jump expression. If you are new to Kotlin and have yet to write your first program in the language, check out our other article on Kotlin syntax and first program

Kotlin has three structural jump expressions: returnbreak and continue. We will also look at labels. Labels are identifier that includes the @ sign followed by a label name, and you can place a label in front of any expression to make it a label. We will also see how they can be used with break and continue expressions to alter the control flow of the program.

Return

It is generally used in function declarations to return values after a function has been executed. A return statement terminates a function's execution and hands control back to the calling function. Execution continues in the calling function at the point immediately following the call.

In the program below, we calculate the sum of all the array’s elements and return it as the function’s output. 

// Function to calculate the sum of all the array elements
fun calculateSum(arr: Array<Int>) : Int {
    var sum=0 // Initialising the variable sum with zero value
    for(i in arr){ // Traversing the array
        sum+=i
    }
    return sum // Returning Sum
}

fun main(){
    var arr = arrayOf<Int>(1,2,3,4) // Initialising an array
    var res= calculateSum(arr) // Calling calculateSum function to calculate the sum of all elements of array
    print(res)
}

Output:

10

In the below example, the function will return when the total sum exceeds 5. In the previous example, we used a return statement to return the function's output, but in this case, we will use a return statement to stop the function's execution. 

// Function to calculate the sum of all the elements of an array. 
// This function will return when the total sum exceeds 5. 
fun calculateSum2(arr: Array<Int>){
    var sum=0 // Initialising the variable sum with zero value
    for(i in arr){ // Traversing the array
        if(sum>5)return // Function returns if sum goes above 5
        sum+=i
        println(sum) // Printing the current sum
    }
}

fun main(){
    var arr = arrayOf<Int>(1,1,2,4,5) // Initialising an array
    // Calling calculateSum2 function to find the sum of the array. It will return when the sum exceeds 5.
    calculateSum2(arr)
}

Output:

1
2
4
8

So, this is how the return statement works. 

Break

break statement is used to stop the flow of a loop. However, it only terminates the nearest enclosing loop. For example, if you have two nested for-loops and the break statement is included in the inner for-loop, the inner for-loop will be ended first, followed by the outer for-loop if another break is added. For example,

// Function with two nested loops. The inner loop will break when j equals 3.
fun main(){
    for(i in 1..4){
        for(j in 1..4){
            // Break loop if the value of j is 3
            if(j == 3)break
            println("i=$i, j=$j")
        }
    }
}

Output:

i=1, j=1
i=1, j=2
i=2, j=1
i=2, j=2
i=3, j=1
i=3, j=2
i=4, j=1
i=4, j=2

In this example, we have put a break statement in the inner loop, which will break when the value of j becomes equal to 3. As you can see in the output, the break statement only affects the inner for-loop here as we have only used one break statement. An additional break statement can be used to break the outer loop also.

Continue

It is very similar to a break statement. The sole difference between it and a break statement is that a break statement ends the loop entirely, whereas a continue statement only skips the current iteration. For example, 

// This function skips all the even values and only prints the odd values. 
fun main(){
    for( i in 1..10){
        if(i%2 == 0)continue // Skips all the even values
        println("i: $i")
    }
}

Output:

i: 1
i: 3
i: 5
i: 7
i: 9

Labels

label is an identifier that includes the @ sign, such as abc@ or foo@. We just place a label in front of an expression to make it a label. Labels can also be used with break and continue statements.

Labeled break statement

Using a labeled break statement, you can also break outer loops instead of only breaking the nearest enclosing loop. For example,

// This function will break the loop with outerloop label when i equals j.
fun main(){
    outerloop@ for(i in 1..5){ // Labelling this loop
        for(j in 2..5){
            println("i: $i, j:$j")
            // break the outer loop when i is equal to j
            if(i==j)break@outerloop
        }
    }
}

Output:

i: 1, j:2
i: 1, j:3
i: 1, j:4
i: 1, j:5
i: 2, j:2

In the above example, when the value of i and j becomes equal, the outer loop breaks, which stops the execution of both the loops. 

Labeled continue statement

Labeled continue statements are used to repeat a specific loop (labeled loop). We can also skip an interaction of any labeled loop using it. For example,

// This function will skip the outer loop iteration when i equals to 2.
fun main(){
    outerloop@ for(i in 1..3){ // Labelling this loop
        for(j in 1..3){
            if(i==2)continue@outerloop
            println("i: $i, j:$j")
        }
    }
}

Output:

i: 1, j:1
i: 1, j:2
i: 1, j:3
i: 3, j:1
i: 3, j:2
i: 3, j:3

In this example, we skip a whole iteration where i equals 2 using a labeled continue statement in this example. 

FAQs

  1. What is the main difference between a break and a continue statement?
    A break statement terminates the entire loop, while a continue statement only skips the current iteration. 
     
  2. What is the purpose of using jump expressions?
    We use jump expressions like return, break, and continue to alter the control flow and get the desired output. 
     
  3. Where else can you use a break statement other than a for-loop?
    Break statements can be used in all types of loops. We can use break statements in a while-loop and a do-while loop also. 

Key Takeaways  

Cheers if you reached here!!!

In this blog, we discussed returnbreak, and a continue expression. Additionally, we looked at labels and how they can be used with jump expressions to better control the flow of execution. 

After reading this blog, I believe you will be able to work with Kotlin jump expressions smoothly. If you wish to learn how jump expressions are used with Kotlin Loops, check out my other article on Kotlin Loops

Check out this problem - Two Sum Problem

Live masterclass