In this article, we will learn how to use continue in kotlin. We will learn how to use continue statement in for loop, while loop,do while loop. Also, we will go through different examples for each loop. At last, you will see different questions based on the topics covered in this tutorial. So, if you have any question, do not forget to comment below. We will be glad to answer your question. At last, we will see labeled continue in kotlin.
When we work with loop in the the program, sometimes, we need to skip the current iteration of the loop. In that situation, we need to use continue statement in the program. Using continue statement, we can skip the current iteration of the enclosing loop in the program.
There are 2 ways to use continue in kotlin programming language.
a. Without Label(UnLabeled Continue)
b. With Label(Labeled Continue)
Now, we will go through each type one by one.
How to use unlabeled continue in kotlin(Or Continue in Kotlin)
Unlabeled continue (or simply continue) in kotlin skips the current iteration of the nearest enclosing loop in the program.
You can use continue in while loop, do while loop and for loop in the program. We will go through each of them one by one.
Flow Diagram to use continue in while loop in kotlin
Syntax to use continue in while loop in kotlin
Let’s see the syntax to use continue statement in while loop.
Example to Show continue in while loop in kotlin
Now, we are going to see different examples that clearly demonstrates that use of continue in while loop in kotlin.
1. Example to show use of kotlin continue statement.
fun main(args: Array<String>) { var i = 10 while (i >= 0) { if (i % 2 == 0) { i--; continue; } println(i); i--; } }
When you run the program, the output will be –
9
7
5
3
1
Here, we are checking if i is divisible by 2 or not. If yes, value of i is decremented by 1. Then, continue option
will skip the current iteration of the loop.
2. Example to show use of kotlin continue statement.
fun main(args: Array<String>) { var i = 5 while(i != 0){ if(i == 0) continue; println(i); i -= 2; } }
When you run the program, you will get below output
5
3
1
-1
-3
…. and so on
i is never 0. Value of i will be 5, 3, 1, -1, -3 and so on…So, continue statement will never be executed. Note that while loop is never terminating.
3. Guess the output of below program
fun main(args: Array<String>) { var i = 10 while (i >= 0) { i--; continue; println(i * i * i) } }
Flow Diagram to use continue in do while loop in kotlin
Syntax to use continue in do while loop in kotlin
Example to Show 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. Example to show use of kotlin continue statement.
fun main(args: Array<String>) { val i = 0 do { continue println(i); } while (i == 1); }
When you run the program, you will get below output –
Nothing will be printed.
Continue will cause the control of the program to skip current iteration. Then, while condition will be checked.
do while loop is terminated because i == 1 is false.
2. Example to demonstrate continue statement in kotlin
fun main(args: Array<String>) { var j = 10 do { if (j >= 5) { j-- continue } println("$j") j-- } while (j > 0) }
When you run the program, you will get below output –
4
3
2
1
3. Example to demonstrate continue statement in kotlin
fun main(args: Array<String>) { var j = 10 do { if (j >= 5) { j-- continue } println("j = $j") j-- } while (j > 0) }
When you run the program, you will get below output –
10
9
8
7
Now, we have some questions for you.
1. Guess the output of below program
fun main(args: Array<String>) { val x = 12 var y = 1 do { if (y > 0) continue if (x > 5) println("ONE") else if (x > 10) println("TWO") else if (x == 15) println("THREE") else println("FOUR") } while (y-- != 0) }
2. Guess the output of below program
fun main(args: Array<String>) { val x = 1 do { if (true) continue println("Hi Tutorialwing"); } while (x > 0); }
3. Guess the output of below program
fun main(args: Array<String>) { val x = false do { if (x) continue println("Hi Tutorialwing"); } while (x); }
Flow Diagram to use continue in for loop in kotlin
Syntax to use continue in for loop in kotlin
Example to Show Continue in for loop in kotlin
Now, we will see different examples that demonstrates use of continue in for loop in kotlin.
1. Example to demonstrate continue statement in kotlin
fun main(args: Array<String>) { for (i in 1..6) { if (i == 5) continue println(i) } }
When you run the program, you will get below output –
1
2
3
4
6
At i = 5, continue will cause control of the program to jump at end of the while loop. So, println() won’t be executed.
2. Example to demonstrate continue statement in kotlin
fun main(args: Array<String>) { for (i in 6 downTo 1 step 2) { if(i > 1) continue println(i) } }
When you run the program, you will get below output –
Nothing will be printed.
Guess what.. println() will never be executed. Value of i will decrease as 6, 4, 2.
3. Guess the output of below program
fun main(args: Array<String>) { val books = arrayOf("Harry Potter", "Black Beauty", "The Alchemist", "The Ginger Man", "The Hite Report") for (i in books.indices) { if(i > 1) continue println(books[i]) } }
How to use Labeled Continue in Koltin?
As we know very well, you can skip current iteration of nearest enclosing loop in the program using continue statement. What if you want to skip current iteration of the desired loop if certain conditions are satisfied? How would you achieve it?
Well the answer is labeled continue. You can skip current iteration of desired loop using labeled continue. This is one of the brilliant feature of kotlin programming language. You just need to provide continue@labelname to skip the current iteration of desired loop with label labelname.
2. Example to demonstrate labeled continue in kotlin
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") } } }
When you run the program, you will get below output –
i = 1; j = 1
i = 1; j = 2
i = 1; j = 3
i = 1; j = 4
i = 1; j = 5
i = 2; j = 1
i = 2; j = 2
i = 2; j = 3
i = 2; j = 4
i = 2; j = 5
We are checking if i is greater than 2 or not. If YES, current iteration of the first for loop is skipped and next iteration is started.
Conclusion
In this post, we have seen how to use continue in Kotlin.We have learnt how to use continue in for loop, while loop or do while loop in kotlin. We have also gone through labeled continue in kotlin.
You must be logged in to post a comment.