Kotlin continue Statement

Jump statements are evident to us from the last topic in which we discovered the use of break condition which can be used with or without labels. This topic can be taken as an extension of the same. Break is a statement which is used to end or terminate the flow of control once it enters loop, but what if we want to keep on executing the flow but after skipping the current flow. Let’s take an example, for all students who are failing we have to grace marks if difference is less than 3, we don’t need to perform the calculation if the student is pass. Let’s have a look at syntax of continue to understand its working.

Kotlin continue Statement

Syntax Of Continue:

while (<Condition to be tested Loop>) {
	// Loop code Part - 1
	if (<Conditional Statement If>) {
		continue
	}
	
	// Loop code Part - 2
}

In the above case, if <Conditional Statement If> is TRUE, the remaining part of loop would be skipped i.e. Loop code Part – 2 would not be executed.

Kotlin continue Statement

Image Source

Let’s see how it executes with a simple example to print sum of first 10 natural numbers except for 5.

fun main(args: Array<String>) {
    var i = 0
    var num = 5
    var sum = 0
    while (i<10) {
		i++
		if (num == i) {
			continue
		}
		sum = sum+i
        // Loop code Part - 2
	}
    println("The sum of numbers leaving 5 is : $sum")
}

Output

The sum of numbers leaving 5 is: 50

So, in simple words it skipped the loop body after continue was called. Now lets try to execute the same sum of first 10 natural numbers skipping the number 5 using for loop, just to understand how continue works with the other loops.

fun main(args: Array<String>) {
    var num = 5
    var sum = 0
    
    for (i in 0..10) {
        if (num == i) {
            continue
        }
        
        sum = sum+i
        //Loop code Part - 2
	}
    
    println("The sum of numbers leaving 5 is : $sum")
}

The output of above code is same as the one which we got in while loop, to imply the statement to calculate the sum “sum = sum+i” is called in all cases when the value of i not equal to 5.

The case we saw above is unlabeled continue statement, since it simply continues with the execution of the current loop. But if we want to skip the iteration of an outer loop we can use continue with label. As discussed in the case of break statements, labels are simple identifier which are followed by ‘@’.

Syntax of Label

Identifier1@

Example: outerLoop@

Though continue works well with label and is supported, still from the coding perspective it is often advised to avoid the use of labels with continue as it makes code loop a bit more complicated.

Let’s have a look at how it looks to use Labelled Continue.

fun main(args: Array<String>) {
  outerLoop@ for (i in 1..2) {	 
      for(j in 3 downTo 1){
          if(i == j){
              println("Continue would be called now.")
              continue@outerLoop   
          }
          println("Printing Value Of : $i & $j")
      }  
  }
}

Output

Printing Value Of : 1 & 3
Printing Value Of : 1 & 2
Continue would be called now.
Printing Value Of : 2 & 3
Continue would be called now.

So that was all about about Kotlin continue statement, now after this we move on to the next Kotlin Jump Statement in the next tutorial.

Leave a Comment

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