Open In App

Kotlin labelled continue

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to use continue in Kotlin. While working with a loop in the 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. 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 continuing in Kotlin- 
As we know, unlabelled 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 a label in front of the expression and calls it using continue@abc.
We are going to learn how to use labeled continue in a while, do-while, and for a 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 the 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.
The 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-  

Kotlin




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--
        }
    }
}


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 the do-while loop. In the below program, we have used a nested do-while loop and labeled the outer loop with outer@ and inner loop with inner@. The condition for continue passes in an inner do-while loop. If the condition becomes true then continue@outer skips the following statements or expressions and passes the control to outer do-while for repetition.
The syntax for labeled continue in the do-while loop- 
 

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

Kotlin




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)
}


Output: 
 

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

 Use of labelled 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. 
The syntax for labelled 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- 

Kotlin




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")
        }
    }
}


Output: 
 

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

 



Last Updated : 28 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads