Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The break expression in Kotlin is a structural jump control flow operator used to prematurely terminate the execution of an enclosing loop (for, while, or do-while). Upon execution, control is immediately transferred to the statement directly following the terminated loop. Because it unconditionally alters control flow, the expression evaluates to the Nothing type. Kotlin supports two forms of the break expression: unlabeled and labeled.

Unlabeled Break

An unlabeled break terminates the nearest (innermost) enclosing loop in the lexical scope.
fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    
    for (i in numbers) {
        if (i == 3) {
            break // Terminates the innermost loop immediately
        }
        println(i) // Subsequent statements in the current iteration are skipped when i == 3
    }
    // Execution resumes here. Output will be 1, 2.
}

Labeled Break

A labeled break is used to terminate a specific loop within a nested loop structure. A label in Kotlin is defined by an identifier followed by an @ sign (e.g., outer@). The break expression is then appended with the same label (break@outer). When a labeled break is invoked, execution jumps to the statement immediately following the loop designated by that label.
fun main() {
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (i == 2 && j == 2) {
                break@outer // Terminates the 'outer@' loop entirely
            }
            println("i=$i, j=$j")
        }
    }
    // Execution resumes here. 
    // Output will be i=1, j=1 | i=1, j=2 | i=1, j=3 | i=2, j=1.
}

Technical Constraints and Mechanics

  • Lexical Scope: The break expression must be lexically enclosed by the loop it intends to terminate. It will result in a compiler error if used outside of a loop construct.
  • Lambda Expressions: break is not supported inside lambda expressions passed to higher-order functions (such as Iterable.forEach). To achieve early termination in those contexts, structural loops or labeled return expressions must be used instead.
  • Type System Integration: Because break evaluates to Nothing, it acts as a bottom type. This means it can be used as a branch in expressions where a specific type is expected, provided the break itself remains lexically scoped within a loop. The compiler allows this because it guarantees the assignment or subsequent evaluation will never be reached.
fun main() {
    val tokens = listOf("valid", "valid", "error", "valid")
    
    for (token in tokens) {
        // Valid syntax: 'break' evaluates to 'Nothing', satisfying the 'String' type requirement
        val result: String = if (token != "error") {
            "Processed: $token"
        } else {
            break 
        }
        
        println(result)
    }
}
Master Kotlin with Deep Grasping Methodology!Learn More