Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
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
:
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 }
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.
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.
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.
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.
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.
Using labels with continue in Kotlin:
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") } }
Skipping iterations with labeled continues in Kotlin:
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") } }
Labeled continues vs regular continues in Kotlin:
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") } }
Labeling and scoping in Kotlin continue statements:
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") } }
Error handling with labeled continues in Kotlin:
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 } }
Labeled continues in Kotlin and control flow:
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") } }
Nested loops and labeled continues in Kotlin:
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") } }
Labeled continues in functional programming with Kotlin:
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") }
Continuing from a specific loop iteration with labels in Kotlin:
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") } }
Labeling continues for readability in Kotlin:
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") } }