In this article, you will learn when to use keywords like continue and break.

Continue

Continue keyword is used in a loop when you want to immediately skip the current iteration and jump to the next iteration. Let’s look at an example.

fun main(args: Array<String>) {    
    val numbers = arrayOf(1,2,3,4)
    for( element in numbers) {
        println("Before-- $element")
        if(element == 2) 
           continue
        println("After -- $element")
    }
}

// output
/* 
* Before -- 1
* After -- 1
* Before -- 2
* Before -- 3
* After -- 3
* Before -- 4
* After -- 4
*/

As you can see in the result when the element is 2, the current iteration is skipped so the statements after continue are skipped. The control is transferred to the next iteration.

Continue with Label

When there are multiple loops, we can specify the loop we want to skip by using labels. We can put the label in front of a loop we want to bypass, and use this label along with the continue to specify that this is the loop that we want to skip. Let’s look at an example where we have two loops and we want to go to the next iteration of the outer loop. 

fun main(args: Array<String>) {
    val numbers = arrayOf(1,2,3,4)
    mylabel@ for (i in 0..5) {
        for ( element in numbers) {
             if( element == 2) {
                 continue@mylabel
             }
             println("loops i = $i and element = $element")
         }
    }
}
// output 
/*
* Loops i = 0 and element = 1
* Loops i = 1 and element = 1
* Loops i = 2 and element = 1
* Loops i = 3 and element = 1
* Loops i = 4 and element = 1
* Loops i = 5 and element = 1
*/

In the above example, as soon as the element 2 appears the control is passed to the next iteration of the outer loop. 

Break

Break is another keyword used very often with loops. When the break is executed the current loop reaches the end of it. Let’s take the fig. 1 and replace the continue with break and observe the result.

fun main(args: Array<String>) {

    val numbers = arrayOf(1, 2, 3, 4)
    for ( element in numbers) {
        println("Before -- $element")
        if( element == 2) {
            break
        }
        println("After -- $element")
   }
}
// output 
/*
* Before = 1
* After = 1
* Before = 2 
*/

As you can see that once the condition is satisfied and break is executed and the loop terminates.

Let’s take another example with multiple loops to understand how the break statement works.

fun main(args: Array<String>) {
    val numbers = arrayOf(1,2,3,4)
    for (i in 0..5) {
        for ( element in numbers) {
             if( element == 2) {
                 break
             }
             println("loops i = $i and element = $element")
         }
    }
}
// output 
/*
* Loops i = 0 and element = 1
* Loops i = 1 and element = 1
* Loops i = 2 and element = 1
* Loops i = 3 and element = 1
* Loops i = 4 and element = 1
* Loops i = 5 and element = 1
*/

Break with loops

Similar to continue, we can use a label with a break statement, and it is used to specify which loop to exit. Let’s replace the continue statement from fig. 2 with a break statement.

fun main(args: Array<String>) {
    val numbers = arrayOf(1,2,3,4)
    mylabel@ for (i in 0..5) {
        for ( element in numbers) {
             if( element == 2) {
                 break@mylabel
             }
             println("loops i = $i and element = $element")
         }
    }
}
// output 
/*
* Loops i = 0 and element = 1
*/

You can see that the outer loop terminates as soon as the element is 2. Click on the next button to continue reading.

Category
Tags

No responses yet

Leave a Reply

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