Checkout the jumb statements in kotlin

There are three jump expressions in Kotlin.

  • break
  • continue
  • return

Break

A break expression is used for terminate the nearest enclosing loop.

Syntax

  1. for(..){
  2. //body of for
  3. if(checkCondition){
  4. break;
  5. }
  6. }

Above for loop terminates its loop when ifcondition execute break expression.

Kotlin break example

  1. fun main(args: Array) {
  2. for (i in 1..5) {
  3. if (i == 3) {
  4. break
  5. }
  6. println(i)
  7. }
  8. }

Kotlin Labeled break Expression

Labeled is the form of identifier followed by the @ sign, for example abc@, test@. To make an expression as label, we just put a label in front of expression.

Kotlin labeled break expression is used to terminate the specific loop. This is done by using break expression with @ sign followed by label name (break@loop).

Kotlin labeled break example

  1. fun main(args: Array) {
  2. loop@ for (i in 1..3) {
  3. for (j in 1..3) {
  4. println(“i = $i and j = $j”)
  5. if (i == 2)
  6. break@loop
  7. }
  8. }
  9. }
Categories: Android

Leave a Reply

Your email address will not be published. Required fields are marked *

Social Media Auto Publish Powered By : XYZScripts.com