Introduction
In programming, loops are pivotal for executing a block of code repeatedly. However, there are times when you want to skip certain iterations based on specific conditions. That's where the continue statement comes into play. It allows you to skip the remaining code in the current iteration of a loop and proceed to the next iteration.
Why Does It Matter?
The continue statement is particularly useful when you need to filter out unwanted values or conditions while iterating. For instance, if you want to ignore negative numbers in a list while summing up only the positive ones, continue can simplify your logic.
Concept Explanation
Think of the continue statement as a traffic light. When the light is green, you keep moving (the loop continues). However, if you hit a red light (a specific condition), you skip to the next green light (the next iteration of the loop) without stopping to look around.
When and Where to Use `continue`
- Data Filtering: When processing collections where certain conditions are irrelevant.
- Nested Loops: In complex loops where you only want to skip iterations in the inner loop.
- Improving Readability: When you want to avoid deeply nested code structures.
Syntax Section
The basic syntax of the continue statement is straightforward. Here’s how it appears within loop structures:
for (item in collection) {
if (condition) {
continue
}
// Code to execute if the condition is false
}
Breakdown of the Syntax
-
for (item in collection): This part initiates a loop that iterates over each item in the specified collection. -
if (condition): This condition checks whether to skip the current iteration. -
continue: Invokes thecontinuestatement, skipping the rest of the loop body for that iteration.
Multiple Working Examples
Example 1: Basic Usage of `continue`
fun main() {
for (number in 1..5) {
if (number == 3) {
continue // Skip the rest of the loop when number is 3
}
println("Current number: $number")
}
}
Output:
Current number: 1
Current number: 2
Current number: 4
Current number: 5
Example 2: Filtering Negative Numbers
fun main() {
val numbers = listOf(-1, 2, -3, 4, 5)
for (number in numbers) {
if (number < 0) {
continue // Skip negative numbers
}
println("Positive number: $number")
}
}
Output:
Positive number: 2
Positive number: 4
Positive number: 5
Example 3: Nested Loops with `continue`
fun main() {
for (i in 1..3) {
for (j in 1..3) {
if (j == 2) {
continue // Skip when j is 2
}
println("i = $i, j = $j")
}
}
}
Output:
i = 1, j = 1
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 3
Example 4: Using `continue` with Labeled Loops
Sometimes, you may want to skip to the next iteration of an outer loop from within a nested loop. This is where labeled continue comes in.
fun main() {
outerLoop@ for (i in 1..3) {
for (j in 1..3) {
if (i == 2) {
continue@outerLoop // Skip to the next iteration of the outer loop
}
println("i = $i, j = $j")
}
}
}
Output:
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3
Comparison of Regular `continue` vs. Labeled `continue`
| Aspect | Regular continue |
Labeled continue |
|---|---|---|
| Affects | Only the nearest loop | Affects a specific labeled loop |
| Syntax | continue |
continue@labelName |
| Use Case | Simple loops | Nested loops with specific control |
Common Mistakes
Mistake 1: Forgetting to Use `continue` Correctly
fun main() {
for (i in 1..5) {
if (i == 3)
continue // This will cause a compilation error if not in a loop
println(i)
}
}
Correction:
Ensure that the continue statement is placed within a loop context.
Mistake 2: Misunderstanding Labeled `continue`
fun main() {
outer@ for (i in 1..3) {
for (j in 1..3) {
if (j == 2) {
continue // This continues the inner loop, not the outer one
}
println("i = $i, j = $j")
}
}
}
Correction:
Use continue@outer to correctly skip to the next iteration of the outer loop.
Best Practices
- Use Clear Labels: When using labeled continues, ensure your labels are meaningful to improve code readability.
- Limit Nesting: Avoid excessively nested loops; consider refactoring if you find yourself needing to use labeled continues frequently.
- Comment Your Code: Always comment when using
continue, especially in complex loops, to clarify your intentions to others (and your future self).
Practice Exercises
- Filter Even Numbers: Write a program that prints only odd numbers from 1 to 10 using the
continuestatement. - Skip Specific Values: Create a loop that skips numbers divisible by 5 while counting from 1 to 20.
- Nested Loop Filtering: Write a nested loop that prints pairs of numbers from two ranges, but skips pairs where the first number is greater than the second.
Feel free to experiment with these exercises to deepen your understanding of the continue statement in Kotlin!