Sling Academy
Home/Kotlin/Using Labels with `break` and `continue` in Kotlin

Using Labels with `break` and `continue` in Kotlin

Last updated: November 30, 2024

Kotlin provides powerful control flow expressions like break and continue that allow developers to manage the execution of loops more precisely. When dealing with nested loops, these expressions can be utilized alongside labels to improve the readability and control of your code. This article will guide you through using labeled break and continue statements in Kotlin with multiple examples.

Understanding Labels in Kotlin

In Kotlin, a label is an identifier followed by the @ sign. You can define a label before any expression and use it to refer to that specific expression elsewhere. Labels are primarily used with functions and control flow jumps like break and continue.

Syntax of Labels

labelName@ // Example of a label declaration

Using Labeled break in Kotlin

The break statement is used to exit the nearest enclosing loop. However, in the case of nested loops, Kotlin allows you to break out of a specific loop by using labels.

Example of Labeled break


fun main() {
    outerLoop@ for (i in 1..5) {
        for (j in 1..5) {
            println("i = $i, j = $j")
            if (j == 3) break@outerLoop
        }
    }
    println("Loop ended")
}

In this example, the break@outerLoop breaks out of the outerLoop even though it's inside the inner loop when j is 3.

Using Labeled continue in Kotlin

The continue statement is used to skip the current iteration of the nearest enclosing loop. With labeled continue, you can skip the iteration of the specified loop instead.

Example of Labeled continue


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

In this example, continue@outerLoop causes the loop to skip the rest of its body and move to the next iteration of outerLoop when j is 2.

Benefits of Using Labels

  • Improved Code Readability: Labels make your intention to exit or continue a specific loop clear, especially in deeply nested loops.
  • Precise Control: They allow a more granular control over which loop should be affected by the break or continue.

Conclusion

Leveraging labels with break and continue in Kotlin can make your code more readable and provide fine-grained control over loop behavior. Use labels judiciously to maintain clarity and simplicity in your code structure.

Next Article: How to Skip Iterations with `continue` in Kotlin

Previous Article: What Does `continue` Do in Kotlin Loops?

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