Kotlin continue Statement

Introduction

The continue statement in Kotlin is used to skip the current iteration of the nearest enclosing loop and proceed to the next iteration. This is useful when you want to skip certain iterations based on a specific condition. This chapter will cover the syntax and usage of the continue statement in Kotlin with examples.

Basic continue Statement

The continue statement can be used inside any loop (for, while, or do-while). When the continue statement is executed, the loop skips the remaining code in the current iteration and proceeds to the next iteration.

Syntax

continue

Example with for Loop

fun main() {
    for (i in 1..10) {
        if (i % 2 == 0) {
            continue
        }
        println("Iteration $i")
    }
}

Explanation:

  • The loop iterates from 1 to 10.
  • When i is even, the continue statement is executed, and the loop skips to the next iteration.
  • The output will be:
    Iteration 1
    Iteration 3
    Iteration 5
    Iteration 7
    Iteration 9
    

Example with while Loop

fun main() {
    var i = 1
    while (i <= 10) {
        if (i % 2 == 0) {
            i++
            continue
        }
        println("Iteration $i")
        i++
    }
}

Explanation:

  • The loop iterates while i is less than or equal to 10.
  • When i is even, the continue statement is executed, and the loop skips to the next iteration.
  • The output will be:
    Iteration 1
    Iteration 3
    Iteration 5
    Iteration 7
    Iteration 9
    

Example with do-while Loop

fun main() {
    var i = 1
    do {
        if (i % 2 == 0) {
            i++
            continue
        }
        println("Iteration $i")
        i++
    } while (i <= 10)
}

Explanation:

  • The loop iterates while i is less than or equal to 10.
  • When i is even, the continue statement is executed, and the loop skips to the next iteration.
  • The output will be:
    Iteration 1
    Iteration 3
    Iteration 5
    Iteration 7
    Iteration 9
    

Nested Loops

When using nested loops, the continue statement only affects the nearest enclosing loop. To skip iterations in an outer loop, you can use labels.

Example with Nested Loops

fun main() {
    for (i in 1..3) {
        for (j in 1..3) {
            if (j == 2) {
                continue
            }
            println("i = $i, j = $j")
        }
    }
}

Explanation:

  • The outer loop iterates from 1 to 3.
  • The inner loop iterates from 1 to 3 for each iteration of the outer loop.
  • When j equals 2, the continue statement is executed, and the loop skips to the next iteration of the inner loop.
  • The output will be:
    i = 1, j = 1
    i = 1, j = 3
    i = 2, j = 1
    i = 2, j = 3
    i = 3, j = 1
    i = 3, j = 3
    

Labeled continue Statement

To skip iterations in an outer loop, you can use labeled continue statements. A label is defined by placing an identifier followed by @ before the loop.

Syntax

label@ for (item in collection) {
    for (subItem in subCollection) {
        if (condition) {
            continue@label
        }
    }
}

Example with Labeled continue

fun main() {
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (i == 2 && j == 2) {
                continue@outer
            }
            println("i = $i, j = $j")
        }
    }
}

Explanation:

  • outer@ is a label for the outer loop.
  • When i equals 2 and j equals 2, the continue@outer statement is executed, and the loop skips to the next iteration of the outer loop.
  • The output will be:
    i = 1, j = 1
    i = 1, j = 2
    i = 1, j = 3
    i = 2, j = 1
    i = 3, j = 1
    i = 3, j = 2
    i = 3, j = 3
    

Example Program with continue Statement

Here is an example program that demonstrates the use of the continue statement in Kotlin:

fun main() {
    // Basic continue statement in a for loop
    println("Basic continue statement in a for loop:")
    for (i in 1..10) {
        if (i % 2 == 0) {
            continue
        }
        println("Iteration $i")
    }

    // continue statement in a while loop
    println("\nContinue statement in a while loop:")
    var j = 1
    while (j <= 10) {
        if (j % 2 == 0) {
            j++
            continue
        }
        println("Iteration $j")
        j++
    }

    // continue statement in a do-while loop
    println("\nContinue statement in a do-while loop:")
    var k = 1
    do {
        if (k % 2 == 0) {
            k++
            continue
        }
        println("Iteration $k")
        k++
    } while (k <= 10)

    // Nested loops with continue statement
    println("\nNested loops with continue statement:")
    for (i in 1..3) {
        for (j in 1..3) {
            if (j == 2) {
                continue
            }
            println("i = $i, j = $j")
        }
    }

    // Labeled continue statement
    println("\nLabeled continue statement:")
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (i == 2 && j == 2) {
                continue@outer
            }
            println("i = $i, j = $j")
        }
    }
}

Conclusion

In this chapter, you learned about the continue statement in Kotlin, including its syntax and usage to skip the current iteration of a loop and proceed to the next iteration. You also saw examples of using the continue statement in for, while, and do-while loops, as well as handling nested loops with labeled continue statements. Understanding how to use the continue statement is essential for controlling the flow of your Kotlin programs and managing loops effectively.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top