In this section, we will learn what the continue statement is and how to use it in Kotlin.
continue Statement in Kotlin
The continue statement can be used within the body of loops when we want to skip the current iteration of a loop and continue to run its next iteration.
In short, the continue statement is a way of terminating only the current iteration of a loop! Note that we’re emphasizing on the words Current Iteration
. This means the loop continues to iterate, but only its current iteration will be terminated as a result of calling the continue statement.
Kotlin continue Statement Syntax:
continue
The continue statement doesn’t need a value! Only put this statement in a loop and it will stop the current iteration of the loop and make the program to run the next iteration of that loop (if any iteration remained in that loop of course).
Example: for loop continue Statement
fun main(){ for (num in 0..100){ if (num <95){ continue } println("The value of the num variable is: ${num}") } println("This message is coming after the for loop") }
Output:
The value of the num variable is: 95 The value of the num variable is: 96 The value of the num variable is: 97 The value of the num variable is: 98 The value of the num variable is: 99 The value of the num variable is: 100 This message is coming after the for loop
Example: while loop continue statement
fun main(){ var num = 0 while (num <100){ num++ if (num <95){ continue } println("The value of the num variable is: ${num}") } println("This message is coming after the for loop") }
Output:
The value of the num variable is: 95 The value of the num variable is: 96 The value of the num variable is: 97 The value of the num variable is: 98 The value of the num variable is: 99 The value of the num variable is: 100 This message is coming after the for loop
Kotlin continue Statement in Nested Loop
The continue statement can be used within the body of a loop, no-matter if that loop is inside another loop or not!
But be aware that when using the continue statement inside the body of an inner loop, only the current iteration of that inner loop will be skipped and not the outer loop!
Basically, the continue statement only affects its enclosing loop, not any loop after that!
Example: nested loop and continue statement
fun main(){ for (num in 0..3){ println("*** The value of the num variable is: ${num}***") for (num2 in 0..4){ if (num2 <=3){ continue } println("The value of the num2 variable is:${num2} ") } } println("This message is coming after the for loop") }
Output:
*** The value of the num variable is: 0*** The value of the num2 variable is:4 *** The value of the num variable is: 1*** The value of the num2 variable is:4 *** The value of the num variable is: 2*** The value of the num2 variable is:4 *** The value of the num variable is: 3*** The value of the num2 variable is:4 This message is coming after the for loop
Here in the inner loop a call to the continue statement occurs as long as the value of the num2
variable is less than the value 3. That’s why no call to the println
function in the inner loop was made until the value of the num2 variable reached to 4.