in , ,

Kotlin continue Expression

Kotlin continue Expression
Kotlin continue Expression

Kotlin continue: In this tutorial, you will learn to use keep on avoiding the current iteration of a loop. Additionally, you will likewise learn about continue labels in this tutorial.

The continue construct skips the current iteration of the loop and jumps the control to end of the loop for the next iteration. The continue is typically used with if else expression to skip the current iteration of the loop for a predetermined condition.

Assume you are working with loops. It is once in a while desirable to avoid the current iteration of the loop.

In such a case, continue is used. The continue construct avoids the current iteration of the enclosing loop, and the control of the program leaps to the furthest limit of the loop body.


How continue works?

It is quite often used with if…else construct. For instance,

while (testExpression1) {

    // codes1
    if (testExpression2) {
        continue
    }
    // codes2
}

On the off chance that the testExpression2 is assessed to true, continue is executed which skips every one of the codes inside while loop after it for that iteration.


Example: Kotlin continue

fun main(args: Array<String>) {

    for (i in 1..5) {
        println("$i Always printed.")
        if (i > 1 && i < 5) {
            continue
        }
        println("$i Not always printed.")
    }
}

At the point when you run the program, the output will be:

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

At the point when the value of I is greater than 1 and less than 5, continue is executed, which avoids the execution of

println("$i Not always printed.")

statement.

In any case, the statement

println("$i Always printed.")

is executed in every iteration of the loop since this assertion exists before the continue construct.


Example: Calculate Sum of Positive Numbers Only

The program beneath calculates the sum of the maximum of 6 positive numbers entered by the user. In the event that the user enters a negative number or zero, it is skipped from the calculation.

Visit Kotlin Basic Input Output to learn more on how to take input from the user.

fun main(args: Array<String>) {

    var number: Int
    var sum = 0

    for (i in 1..6) {
        print("Enter an integer: ")
        number = readLine()!!.toInt()

        if (number <= 0)
            continue
        
        sum += number
    }
    println("sum = $sum")
}

At the point when you run the program, the output will be:

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

Kotlin Labeled continue

What you have discovered till now is the unlabeled type of continue, which skips the current iteration of the closest enclosing loop. continue can likewise be used to avoid the iteration of the desired loop (can be an external loop) by using continue labels.


How labeled continue works?

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

Here, outerloop@ is a name set apart at an external while loop. Presently, by using continue with the label (continue@outerloop for this situation), you can skip the execution of codes of the particular loop for that iteration.


Example: labeled continue

fun main(args: Array<String>) {

    here@ for (i in 1..5) {
        for (j in 1..4) {
            if (i == 3 || j == 2)
                continue@here
            println("i = $i; j = $j")
        }
    }
}

At the point when you run the program, the output will be:

i = 1; j = 1
i = 2; j = 1
i = 4; j = 1
i = 5; j = 1

The use of labeled continue is frequently discouraged as it makes your code hard to understand. In the event that you are in a circumstance where you need to use labeled continue, refactor your code, and attempt to tackle it in an alternate manner to make it more readable.


There are 3 structural jump expressions in Kotlin: break, continue and return. To learn about the break and return expression, visit:

Kotlin break
Kotlin function


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Kotlin break Expression

Kotlin break Expression

Kotlin Functions

Kotlin Function