Labeled Continue In Kotlin With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners

In this article, we will see how to use labeled continue in kotlin programming language. We have seen how to use unlabeled continue (or simply continue) in kotlin programming language in previous post. Also, we have seen how to skip current iteration of nearest enclosing loop if certain conditions are satisfied. Now, we will see how to use labeled continue to skip the current iteration of desired loop if certain conditions are satisfied.

Labeled continue is another type of continue statement in kotlin programming language. You can terminate any labeled loop if certain conditions are satisfied.

Labeled continue in kotlin is mostly used with if statement. An identifier followed by the @ sign is called label. For example, @first, @second, @outer, @inner etc. are some valid labels. In kotlin, you may use label with any expression. You just need to write label in front of it.

You can use labeled continue with while loop, do while loop or for loop in kotlin. We will go through each loop and see how to use continue statement in it.

Syntax to use labeled continue in kotlin in while loop

Below image clearly depicts the syntax to use continue in while loop in kotlin.

Tutorialwing - Syntax of Labeled Continue in while loop in Kotlin

Syntax of Labeled Continue in while loop in Kotlin

Example to show labeled continue in while loop in kotlin

Now, we will see different examples that demonstrates use of continue in while loop in kotlin.

1. Examples to demonstrates the use of labeled continue in kotlin
fun main(args: Array<String>) {
    var i = 5
    first@ while (i > 0) {
        var j = 5
        second@ while (j > 0) {
            if (i == 3)
                continue@second
            println("i = $i; j = $j")
            j--
        }
        i--
    }
}

Output:
i = 5; j = 5
i = 5; j = 4
i = 5; j = 3
i = 5; j = 2
i = 5; j = 1
i = 4; j = 5
i = 4; j = 4
i = 4; j = 3
i = 4; j = 2
i = 4; j = 1

Here, we are checking if i is equal to 3 or not. If YES, we are skipping the current iteration of loop labeled with second label. Note that we are skipping the loop that are labeled. We can also skip the current iteration of loop labeled with first label by writing continue@first.

2. Guess the output of below program
fun main(args: Array<String>) {
    var i = 5
    first@ while (i > 0) {
        var j = 5
        second@ while (j > 0) {
            if (i == 3)
                continue@first
            println("i = $i; j = $j")
            j--
        }
        i--
    }
}
3. Guess the output of below program
fun main(args: Array<String>) {
    var i = 5
    var j = 5
    outer@ while (i > 0) {
        inner@ while (j > 0) {
            if (i == 3)
                continue@outer
            println("i = $i; j = $j")
            j--
        }
        i--
    }
}



Syntax to use labeled continue in kotlin in do while loop

Go through the image to know the syntax to use do while loop in kotlin.

Tutorialwing - Syntax of Labeled Continue in do while loop in Kotlin

Syntax of Labeled Continue in do while loop in Kotlin

Example to show labeled continue in do while loop in kotlin

Now, we will see different examples that demonstrates use of continue in do while loop in kotlin.

1. An example to demonstrate the use of labeled continue in do while loop
fun main(args: Array<String>) {
    var i = 5
    var j = 5
    outer@ do {
        inner@ do {
            if (i == 3)
                continue@outer
            println("i = $i; j = $j")
            j--
        } while (j > 0)
        i--
    } while (i > 0)
}

When you run the program, you will get below output:
i = 5; j = 5
i = 5; j = 4
i = 5; j = 3
i = 5; j = 2
i = 5; j = 1
i = 4; j = 0

This is an example to demonstrates the use of labelled continue in do while loop in kotlin. Here, we are checking if i is equal to 3 or not. If yes, we are skipping the current iteration of the outer do while loop.

2. Guess the output of the below program
fun main(args: Array<String>) {
    var i = 5
    outer@ do {
        var j = 5
        inner@ do {
            if (i == 3)
                continue@outer
            println("i = $i; j = $j")
            j--
        } while (j > 0)
        i--
    } while (i > 0)
}
3. Guess the output of the below program
fun main(args: Array<String>) {
    var i = 5
    outer@ do {
        var j = 5
        inner@ do {
            if (i == 3)
                continue@inner
            println("i = $i; j = $j")
            j--
        } while (j > 0)
        i--
    } while (i > 0)
}

Syntax to use labeled continue in kotlin in for loop

Below image clearly depicts the syntax to use continue in for loop in kotlin.

Tutorialwing - Syntax of Labeled Continue in For Loop in Kotlin

Syntax of Labeled Continue in For loop in Kotlin

Example to show labeled continue in for loop in kotlin

Now, we will see different examples that demonstrates use of continue in for loop in kotlin.

1. An example to demonstrate the use of labeled continue in for loop
fun main(args: Array<String>) {
    first@ for (i in 1..10) {
        second@ for (j in 1..5) {
            if (i > 2) continue@first
            println("i = $i; j = $j")
        }
    }
}
2. Another example to demonstrate the use of labeled continue in for loop
fun main(args: Array<String>) {
    first@ for (i in 1..3) {
        second@ for (j in 1..2) {
            if (i == 2) continue@first
            println("i = $i; j = $j")
        }
    }
}
3. How many times hello will be printed?
fun main(args: Array<String>) {
    outer@ for (i in 1..3) {
        inner@ for (j in 1..6) {
            if (j == 6) continue@outer
            println("Hello")
        }
    }
}



4. How many times Hello will be printed?
fun main(args: Array<String>) {
    first@ for (i in 5 downTo 1 step 2) {
        second@ for (j in 1..2) {
            if (i == 2) continue@second
            println("i = $i, j = $j")
            println("Hello")
        }
    }
}

Conclusion

In this post, we have seen how to use labeled continue in Kotlin. we have also learnt how to use labeled continue in for loop, while loop or do while loop in kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners