Kotlin Tutoial

Basics

Control Flow

Array & String

Functions

Collections

OOPs Concept

Exception Handling

Null Safety

Regex & Ranges

Java Interoperability

Miscellaneous

Android

Kotlin unlabeled continue

In Kotlin, the continue statement is used to skip the current iteration of a loop and move to the next iteration. This can be helpful when you want to bypass specific conditions within a loop without completely exiting the loop itself. When you use the continue statement without a label, it's called an "unlabeled continue."

Let's explore the use of an unlabeled continue statement in Kotlin with some examples.

1. Basic continue in a Simple Loop

Imagine we have an array of numbers and want to print only the odd numbers:

val numbers = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

for (num in numbers) {
    if (num % 2 == 0) {  // If the number is even
        continue  // Skip the rest of this iteration
    }
    println(num)  // Print odd numbers only
}

In this example, when an even number is encountered, the continue statement skips the print command, and the loop proceeds with the next number.

2. continue in Nested Loops

In nested loops, an unlabeled continue statement affects only the innermost loop. Let's look at an example where we have a 2D grid and we want to print elements, but we'll skip any row that contains the number 5:

val matrix = arrayOf(
    arrayOf(1, 2, 3),
    arrayOf(4, 5, 6),
    arrayOf(7, 8, 9)
)

for (row in matrix) {
    for (num in row) {
        if (num == 5) {
            println("Skipping row with number 5!")
            continue  // This will continue the inner loop, not the outer one.
        }
        println("Number: $num")
    }
}

In this example, the continue statement will skip the printing of number 5 but will continue with numbers 6, 7, 8, and 9. It won't skip the entire row because the continue statement only affects the inner loop.

3. Limitations of Unlabeled continue

Just like the unlabeled break, an unlabeled continue only affects the nearest enclosing loop. If you want the continue statement to affect outer loops or specific loops in more intricate control structures, you'd need a labeled continue.

Conclusion

The unlabeled continue statement in Kotlin provides a simple way to skip the current iteration of the nearest loop and proceed with the next one. For situations requiring more complex control over which loop to continue from, Kotlin offers labeled continue statements. The ability to explicitly control the flow of loops with continue aids in crafting more precise and clean logic in your programs.

1. How to use continue in Kotlin loops:

In Kotlin, the continue keyword is used to skip the rest of the current iteration and move to the next one in a loop. It is commonly used to skip certain elements based on a condition. Here's an example using a for loop:

fun main() {
    for (i in 1..5) {
        if (i == 3) {
            continue
        }
        println("Current value: $i")
    }
}

In this example, when i is equal to 3, the continue statement is triggered, skipping the rest of the loop body for that iteration.

2. Skipping iterations with continue in Kotlin:

The continue statement is particularly useful for skipping iterations when a specific condition is met. Here's an example using a while loop:

fun main() {
    var i = 0
    while (i < 5) {
        i++
        if (i == 2 || i == 4) {
            continue
        }
        println("Current value: $i")
    }
}

In this example, when i is 2 or 4, the continue statement is executed, skipping those iterations.

3. Kotlin loop control flow with continue:

The continue statement alters the control flow of loops in Kotlin. It helps you skip certain parts of the loop's body and move on to the next iteration. Here's an example using a do-while loop:

fun main() {
    var i = 0
    do {
        i++
        if (i % 2 == 0) {
            continue
        }
        println("Current value: $i")
    } while (i < 5)
}

In this example, the loop continues to the next iteration when i is an even number.

4. Using continue in for and while loops in Kotlin:

The continue keyword can be used in both for and while loops in Kotlin. Here's an example that demonstrates its usage in both loop types:

fun main() {
    // Using continue in a for loop
    for (i in 1..5) {
        if (i == 3) {
            continue
        }
        println("For loop - Current value: $i")
    }

    // Using continue in a while loop
    var j = 0
    while (j < 5) {
        j++
        if (j == 2) {
            continue
        }
        println("While loop - Current value: $j")
    }
}

This example shows how to use continue in both for and while loops.

5. Kotlin continue vs break:

While continue is used to skip the rest of the current iteration and move to the next one, break is used to exit the loop entirely. Here's an example illustrating the difference:

fun main() {
    for (i in 1..5) {
        if (i == 3) {
            continue // Skips to the next iteration for i = 3
        }
        if (i == 4) {
            break // Exits the loop when i = 4
        }
        println("Current value: $i")
    }
}

In this example, when i is 3, it skips to the next iteration with continue, and when i is 4, it exits the loop with break.

6. Loop iteration control in Kotlin:

The continue statement provides control over loop iterations in Kotlin, allowing you to skip specific iterations based on conditions. Here's a generic example:

fun main() {
    for (i in 1..10) {
        if (i % 2 == 0) {
            continue // Skips even numbers
        }
        println("Current value: $i")
    }
}