Sling Academy
Home/Kotlin/Kotlin: `break` or `continue` Outside of a Loop

Kotlin: `break` or `continue` Outside of a Loop

Last updated: December 01, 2024

When programming in various languages, the break and continue statements provide a way to control the flow of loops. However, Kotlin poses some questions for developers familiar with their use outside of loop constructs. This article dives into how break and continue work in Kotlin, attempts their use outside a loop context, and suggests alternatives when such scenarios arise.

Basic Use of break and continue in Kotlin

First, let’s quickly review how break and continue work within loops in Kotlin:

Using break

The break keyword allows exiting a loop prematurely once a specific condition is fulfilled:


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

In this example, the loop iterates from 1 through 10, but stops execution once i equals 5 due to the break statement.

Using continue

The continue keyword allows skipping the remaining code inside the loop's block, continuing with the next iteration of the loop:


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

Here, the loop skips over even numbers and prints only the odd numbers.

Special Scenario: Labels and Loop Exit

Kotlin introduces the concept of labels, allowing breaks and continues to work with more complex nested loop scenarios:


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

In this context, break@loop1 allows jumping out of the outer loop loop1 instead of just the inner loop.

Challenge: break and continue Outside a Loop

Unlike some other languages, Kotlin does not allow the use of break or continue outside loop constructs. Attempting to do this will result in a compilation error:


fun main() {
    if (true) {
        break // Error: 'break' and 'continue' are not allowed in non-loop contexts
    }
}

This restriction enforces cleaner and more predictable code flow through established control structures like loops.

Alternatives: Refactoring Logic

If you need to break or continue outside of loops, consider alternative control structures:

  • Return Early in Functions: Consider restructuring your logic into functions, and use `return` for exiting the function early:

fun processNumber(num: Int) {
    if (num < 0) {
        return  // Early exit for negative numbers
    }
    // Rest of processing for positive numbers
}
  • Exception Handling: Use custom exceptions if you need an immediate exit from several layers of function calls.

Conclusion

While Kotlin restricts break and continue to loop constructs, these limits help promote more structured and logical flow control. By using alternative strategies like early returns and refactoring logic into separate functions, developers can maintain code clarity and intention. Understanding how these features and alternatives work helps developers write reliable, maintainable Kotlin applications.

Next Article: Kotlin: Can't Return a Value from Void Function

Previous Article: Kotlin: Illegal Character in Identifier Error

Series: Common Errors in Kotlin and How to Fix Them

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