VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/returns-jumps-and-labels-in-kotlin/

⇱ Returns, Jumps and Labels in Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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:

  1. return
  2. break
  3. 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:

Another use of return

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:

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:

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:

That's all about return, jump, and labels. 

Comment
Article Tags:

Explore