Open In App

Kotlin labelled continue

Last Updated : 18 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to use continue in Kotlin. While working with a loop in 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. 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 continuation 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 call it using continue@abc.
We are going to learn how to use labeled continue in a while, do-while, and for loop.

Use of labeled continue in a 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 the inner loop using inner@, then you can easily skip the specific condition using continue@outer in the conditional block.

Syntax for labeled continue in a while loop

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

Kotlin program using labeled continue in a 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 a do-while loop

We can also use the labeled continue in the do-while loop. In the program below, we have used a nested do-while loop and labeled the outer loop with outer@ and the inner loop with inner@. The condition for continuing 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 the outer do-while for repetition.

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 program using labeled continue in a do-while loop

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 a 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 labelled continue in a for loop

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

Kotlin program using continue labeled in a 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

 


Next Article
Article Tags :

Similar Reads