Continue Expression

Suppose you are working with loops. It is sometimes desirable to skip the current iteration of the loop. In such case, continue is used. The continue construct skips the current iteration of the enclosing loop, and the control of the program jumps to the end of the loop body.

How continue works?

Almost always it is used with if..else statements. For example: If the expression is evaluated to true, continue is executed which skips all the codes inside while loop after it for that iteration.

Example


1 Always printed.
1 Not always printed.
2 Always printed.
3 Always printed.
4 Always printed.
5 Always printed.
5 Not always printed.

When the value of i is greater than 1 and less than 5, continue is executed, which skips the execution of the second println statement. However, the statement of the first println is executed in each iteration of the loop because this statement exists before the continue construct.

Example: Calculate Sum of Positive Numbers Only


Enter an integer: 4
Enter an integer: 5
Enter an integer: -50
Enter an integer: 10
Enter an integer: 0
Enter an integer: 12
sum = 31

Here the program calculates the sum of maximum of 6 positive numbers entered by the user. If the user enters negative number or zero, it is skipped from calculation.

Labeled continue

Label in Kotlin starts with an identifier which is followed by @.

Here, test@ is a label marked at outer while loop. Now, by using continue with the label (continue@outerloop in our case), you can skip the execution of codes of the specific loop for that iteration.


A and 1
A and 3
B and 1
B and 3
C and 1
C and 3
D and 1
D and 3

In this example we have a nested for loop and we are not using label. When we do not use labels we do not have any control and as soon as the continue is encountered the current iteration is skipped for the inner loop.


A and 1
B and 1
C and 1
D and 1

We can see in the above example output that for n value 2 and 4 the iteration is skipped for the inner loop. Let’s say we want to skip the iteration for the outer for loop, we can do so with the help of continue labels.

Basically what happened here is that as soon as the n value reached 2 the control jumped to the end of outer loop because of the label and it happened for the each iteration. The syntax of the continue label is pretty straight forward as you just have to give a name to label followed by @ symbol and the same name needs to be appended with the continue statement as shown in the above example.

Questions

I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

 


 

Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

4 thoughts on “Continue Expression”

  1. What i do not realize is actually how you’re not actually much more well-liked than you may be now. You’re very intelligent. You realize therefore significantly relating to this subject, made me personally consider it from numerous varied angles. Its like men and women aren’t fascinated unless it is one thing to do with Lady gaga! Your own stuffs excellent. Always maintain it up!

Leave a Reply

Your email address will not be published. Required fields are marked *