Kotlin labeled continue

In this article, we will learn how to use continue in Kotlin. While working with loop in the programming, sometimes, it is desirable to skip the current iteration of the loop. In that case, we can use continue statement in the program. Basically, continue is used to repeat the loop for a specific condition. It skips the following statements and continues with the next iteration of the loop.

There are two types of continue in Kotlin-

  1. Unlabeled Continue
  2. Labeled Continue

As we know, unlabeled continue is used to skip the iteration of the nearest closing loop but labeled continue is used to skip the iteration of the desired closing loop. It can be with the help of labels like inner@, outer@ etc. We just need to write label in front of the expression and calls it using continue@abc.

We are going to learn how to use labeled continue in while, do-while and for loop.

Use of labeled continue in while loop –

Labeled continue is used to skip the iteration of the desired block when it satisfies a specific condition without checking the condition in while loop. If you mark the outer loop using the label outer@ and inner loop using inner@ then you can easily skip for the specific condition using continue@outer in the conditional block.

Syntax for labeled continue in while loop-

outer@ while(firstcondition) {
      // code
      inner@ while(secondcondition) {
            //code
            if(condition for continue) {
               continue@outer
            } 
      }
}

Kotlin program of using labeled continue in while loop-

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
  
    var num1 = 4
    outer@ while (num1 > 0) {
        num1--
        var num2 = 4
  
        inner@ while (num2 > 0) {
            if (num1 <= 2)
                continue@outer
            println("num1 = $num1, num2 = $num2")
            num2--
        }
    }
}

chevron_right


Output:

num1 = 3, num2 = 4
num1 = 3, num2 = 3
num1 = 3, num2 = 2
num1 = 3, num2 = 1

Use of labeled continue in do-while loop

We can also use the labeled continue in do-while loop. In the below program, we have used nested do-while loop and labeled the outer loop with outer@ and inner loop with inner@. The condition for continue passes in inner do-while loop. If the condition becomes true then continue@outer skips the following statements or expression and passes the control to outer do-while for repetition.

Syntax for labeled continue in do-while loop-

outer@ do {
       // code
       inner@ do {
           // code
           if(condition for continue) {
              continue@outer
           }
       } while(firstcondition)
} while(secondcondition)
filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
  
    var num1 = 4
    outer@ do {
        num1--
        var num2 = 4
  
        inner@ do {
            if (num1 <= 2)
                continue@outer
            println("num1 = $num1; num2 = $num2")
            num2--
        } while (num2 > 0)
  
    } while (num1 > 0)
}

chevron_right


Output:

num1 = 3; num2 = 4
num1 = 3; num2 = 3
num1 = 3; num2 = 2
num1 = 3; num2 = 1

Use of labeled continue in for loop –

We can also use labeled continue in for loop. In the below program, we have used nested for loops and labeled outer loop with outer@ and inner loop with inner@. The condition for continue passes in inner for-loop. If the condition becomes true then it skips the following statements and passes the control to outer for-loop for further iteration.
Syntax for labeled continue in for loop-

outer@ for(iteration through iterator) {
    // code
      inner@ for(iteration through iterator) {
           // code
          if(condition for continue) {
          continue@outer
          }
      }
}

Kotlin program of using continue labeled in for loop-

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    outer@ for (num1 in 4 downTo 1) {
  
        inner@ for (num2 in 4 downTo 1) {
            if (num1 <= 3)
                continue@outer
            println("num1 = $num1; num2 = $num2")
        }
    }
}

chevron_right


Output:

num1 = 4; num2 = 4
num1 = 4; num2 = 3
num1 = 4; num2 = 2
num1 = 4; num2 = 1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.