In this article, you will learn how to use continue construct and continue labels in Kotlin.
Sometimes it is useful to force an early iteration of a loop. That is, you might want to continue running the loop but stop processing the remaining code in its body for this particular iteration. In such case we can use continue statement.
Kotlin continue example
The continue statement is used to skip the iteration of the nearest enclosing loop (for, while, and do…while loop).
Basically, continue is used to repeat the loop for a specific condition. It skips the remaining statements and continues with the next iteration of the loop.
Syntax
Syntax of continue in for loop:
for (iteration through iterator) { //codes if (test-expression) { continue } //codes }
If test-expression is evaluated to true, continue statement is executed which skips the remaining statements inside the loop and continues with the next iteration of the loop.
Example
The below program will print even numbers between 1 to 10.
fun main(args: Array<String>) {
for (n in 1..10) {
if (n % 2 != 0)
continue
print(" $n ")
}
}
Output
2 4 6 8 10
Example explained:
In the above example, when n = { 1, 3, 5, 7, 9 } :
- If condition ( n % 2 ! = 0 ) is checked, yields true.Then, continue statement is executed, which skips the execution of print(” $n “) statement.
Kotlin continue example in nested loop
Example
fun main(args: Array<String>) { for (i in 1..3) { for (j in 1..2) { println("Before continue,i = $i; j = $j") if (i == 2) { continue } println("i = $i; j = $j") } } }
Output
Before continue,i = 1; j = 1 i = 1; j = 1 Before continue,i = 1; j = 2 i = 1; j = 2 Before continue,i = 2; j = 1 Before continue,i = 2; j = 2 Before continue,i = 3; j = 1 i = 3; j = 1 Before continue,i = 3; j = 2 i = 3; j = 2
Example explained:
In the above example, when i = 2 :
- If condition ( i == 2 ) is checked, yields true.Then, continue statement is executed, which skips the execution of println(“i = $i; j = $j”) statement and passes the control to inner for-loop for further iteration.
Kotlin Labeled continue example
In Kotlin, labeled continue is used to skip the iteration of the desired loop when certain condition is satisfied. It can be done with the help of labels.
Label starts with an identifier which is followed by ‘ : ‘. e.g.- inner@ , outer@ etc.
Syntax
Syntax of labeled continue in for loop:
outer@ for(iteration through iterator) { // code inner@ for(iteration through iterator) { // code if(condition for continue) { continue@outer } } }
Example
fun main(args: Array<String>) { label@ for (i in 1..5) { for (j in 1..2) { if (i > 2) { continue@label } println("i = $i; j = $j") } } }
Output
i = 1; j = 1 i = 1; j = 2 i = 2; j = 1 i = 2; j = 2
Example explained:
In the above example, when i = { 3, 4, 5 } :
- If condition ( i > 2 ) is checked, yields true.Then, continue@label is executed, which skips the execution of println(“i = $i; j = $j”) statement and passes the control to the desired loop marked with label label@ for further iteration.
I am beginner in Kotlin and really loving your teaching style. All the concepts are explained with the help of example. On the point lessons.
Thanks Waheed.