Sling Academy
Home/Kotlin/What Does `continue` Do in Kotlin Loops?

What Does `continue` Do in Kotlin Loops?

Last updated: November 30, 2024

In Kotlin, loops such as for, while, and do-while provide mechanisms for iterating over collections, ranges, or executing repeated blocks of code. Optimizing control flow within loops is an essential skill, and one tool that can be used is the continue statement. In this article, we will explore what continue does in Kotlin loops, how it can be applied, and provide code examples for clarity.

Understanding the continue Statement

The continue statement is utilized inside looping constructs to skip the current iteration and move directly to the next one. When a continue statement is encountered within a loop, any code following it within the loop block is ignored for the current iteration only.

Here are some fundamental properties of the continue statement in Kotlin:

  • It works with all loop constructs: for, while, and do-while.
  • It allows the loop to skip to the next iteration without terminating the loop completely.

Using continue with for Loops

Consider the following example where we print numbers from 1 to 10, but skip printing number 5 using continue:


fun main() {
    for (i in 1..10) {
        if (i == 5) {
            continue
        }
        println(i)
    }
}

When this code is executed, numbers 1 to 10 will be printed except for number 5, as the continue statement causes the loop to skip the rest of the block when i is equal to 5.

Using continue with while Loops

The continue statement is used similarly within while loops. Let’s look at an example:


fun main() {
    var i = 0
    while (i < 10) {
        i++
        if (i == 5) {
            continue
        }
        println(i)
    }
}

The logic remains similar; when i is 5, the continue command skips the current iteration, thus avoiding the println statement.

Using continue with do-while Loops

With do-while loops, the continue statement can also be effectively used in the same fashion:


fun main() {
    var i = 0
    do {
        i++
        if (i == 5) {
            continue
        }
        println(i)
    } while (i < 10)
}

As in the previous examples, reaching i == 5 skips the current loop iteration using continue, and the loop proceeds without printing the number 5.

Conclusion

The continue statement is a powerful mechanism for controlling the flow within loops by omitting specific iterations. When conditionally applied, continue statements can facilitate cleaner and more efficient code by bypassing unnecessary operations or conditions efficiently.

By understanding and mastering the use of control statements like continue, you will be able to manipulate loop constructs effectively, making your Kotlin programs more logical and resourceful.

Next Article: Using Labels with `break` and `continue` in Kotlin

Previous Article: How to Use `break` to Exit Loops in Kotlin

Series: Control Flow in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin