> ## 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.

# Kotlin Break

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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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)
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
