Kotlin Tutoial

Basics

Control Flow

Array & String

Functions

Collections

OOPs Concept

Exception Handling

Null Safety

Regex & Ranges

Java Interoperability

Miscellaneous

Android

Kotlin labeled continue

In Kotlin, the continue statement is used to skip the current iteration of a loop and jump to the next iteration. When dealing with nested loops, however, you might sometimes need to control which loop's iteration you want to skip. This is where the labeled continue comes in.

Here's a tutorial on Kotlin's labeled continue:

1. Basic Use of continue:

In a single loop, you can use continue to skip the rest of the loop's current iteration and move to the next one.

for (i in 1..5) {
    if (i == 3) continue
    print(i)  // Outputs: 1245
}

2. Using Labels:

You can label loops in Kotlin and then use that label to specify which loop you want to continue from.

outerLoop@ for (i in 1..3) {
    for (j in 1..3) {
        if (i == 2) continue@outerLoop
        print("($i,$j) ")
    }
    println()
}

In this example, when i is 2, the continue@outerLoop statement is executed. This means the rest of the inner loop for that iteration, as well as the println(), will be skipped, and the next iteration of the outerLoop will start.

3. Importance of Labels in Nested Loops:

If you use continue in a nested loop without a label, it will only affect the nearest enclosing loop. This is why labeled continue becomes essential if you want to control the outer loop.

4. Not Just for Loops:

Although labeled continue statements are primarily used with loops, labels in Kotlin can be applied to other expressions. For instance, they can be helpful in lambda expressions.

5. Caution on Usage:

It's crucial to use labeled continue judiciously. Overusing or misusing it can make the code confusing and harder to maintain. It's often a good idea to leave comments or documentation explaining the logic if it isn't immediately clear.

Conclusion:

The labeled continue in Kotlin provides fine-grained control over the flow of nested loops, allowing developers to specify which loop they want to affect. While powerful, it's essential to use this feature wisely to maintain code readability and clarity.

  1. Using labels with continue in Kotlin:

    • Labels allow for more precise control over the continue statement within nested loops.
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (i == 2 && j == 2) {
                continue@outer  // Skips the current iteration of the outer loop
            }
            println("i: $i, j: $j")
        }
    }
    
  2. Skipping iterations with labeled continues in Kotlin:

    • Labeled continues can skip iterations of a specific loop, enhancing control flow.
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (i == 2 && j == 2) {
                continue@outer  // Skips the current iteration of the outer loop
            }
            println("i: $i, j: $j")
        }
    }
    
  3. Labeled continues vs regular continues in Kotlin:

    • Labeled continues provide more fine-grained control compared to regular continues.
    for (i in 1..3) {
        for (j in 1..3) {
            if (i == 2 && j == 2) {
                continue  // Skips the current iteration of the inner loop only
            }
            println("i: $i, j: $j")
        }
    }
    
  4. Labeling and scoping in Kotlin continue statements:

    • Labels are scoped to the nearest enclosing loop, specifying which loop to continue.
    outer@ for (i in 1..3) {
        inner@ for (j in 1..3) {
            if (i == 2 && j == 2) {
                continue@outer  // Skips the current iteration of the outer loop
            }
            println("i: $i, j: $j")
        }
    }
    
  5. Error handling with labeled continues in Kotlin:

    • Labeled continues can be used for error handling, allowing the skipping of specific loop iterations.
    data class Result(val value: Int, val isError: Boolean)
    
    fun processResults(results: List<Result>) {
        process@ for (result in results) {
            if (result.isError) {
                println("Skipping error: ${result.value}")
                continue@process
            }
            // Process the result
        }
    }
    
  6. Labeled continues in Kotlin and control flow:

    • Labeled continues contribute to a clearer control flow within nested loop structures.
    loop@ for (i in 1..3) {
        for (j in 1..3) {
            if (i == 2 && j == 2) {
                continue@loop  // Skips the current iteration of the loop labeled 'loop'
            }
            println("i: $i, j: $j")
        }
    }
    
  7. Nested loops and labeled continues in Kotlin:

    • Labeled continues are particularly valuable in nested loops for skipping specific iterations.
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (i == 2 && j == 2) {
                continue@outer  // Skips the current iteration of the outer loop
            }
            println("i: $i, j: $j")
        }
    }
    
  8. Labeled continues in functional programming with Kotlin:

    • Labeled continues can be applied in functional programming scenarios for control flow management.
    val numbers = listOf(1, 2, 3, 4, 5)
    
    loop@ for (number in numbers) {
        if (number == 3) {
            continue@loop  // Skips the iteration when number equals 3
        }
        println("Number: $number")
    }
    
  9. Continuing from a specific loop iteration with labels in Kotlin:

    • Labeled continues allow continuing from a specific iteration of an outer loop.
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (i == 2 && j == 2) {
                continue@outer  // Continues from the next iteration of the outer loop
            }
            println("i: $i, j: $j")
        }
    }
    
  10. Labeling continues for readability in Kotlin:

    • Labels enhance code readability by explicitly indicating the loop being continued.
    loop@ for (i in 1..3) {
        inner@ for (j in 1..3) {
            if (i == 2 && j == 2) {
                continue@loop  // Skips the current iteration of the loop labeled 'loop'
            }
            println("i: $i, j: $j")
        }
    }