> ## 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 While Loop

A `while` loop in Kotlin is a control flow statement that repeatedly executes a block of code as long as a specified boolean condition evaluates to `true`. It functions as a pre-test loop, meaning the condition is evaluated before each iteration of the loop body.

```kotlin theme={"dark"}
while (condition) {
    // Loop body
}
```

## Execution Mechanics

1. The `condition` expression is evaluated. In Kotlin, this expression must strictly resolve to a `Boolean` type (`true` or `false`).
2. If the condition evaluates to `true`, the statements within the loop body are executed sequentially.
3. Upon reaching the end of the loop body, control flow jumps back to the condition evaluation.
4. If the condition evaluates to `false`, the loop terminates immediately, and control passes to the next statement following the loop block.

## The `do-while` Variant

Kotlin also provides the `do-while` loop, which operates as a post-test loop. The loop body is executed first, and the condition is evaluated afterward. This guarantees that the loop body will execute at least once, regardless of the initial state of the condition.

```kotlin theme={"dark"}
do {
    // Loop body
} while (condition)
```

**Scoping Note:** A distinct feature of Kotlin's `do-while` loop is that variables declared inside the `do` block remain visible and accessible within the `while` condition expression.

## Loop Control Statements

Kotlin provides structural jump expressions to alter the standard execution flow of `while` and `do-while` loops:

* **`break`**: Immediately terminates the innermost enclosing loop. Control flow is transferred to the statement immediately following the loop.
* **`continue`**: Skips the remaining statements in the current iteration of the loop body. Control flow is transferred directly back to the condition evaluation (in a `while` loop) or the post-test condition (in a `do-while` loop).

## Labeled Jumps

For nested loops, Kotlin supports labels to explicitly define the target of `break` and `continue` expressions. A label is an identifier followed by the `@` sign.

```kotlin theme={"dark"}
outerLoop@ while (conditionA) {
    while (conditionB) {
        if (exitCondition) {
            break@outerLoop // Terminates the outer loop, not just the inner loop
        }
        if (skipCondition) {
            continue@outerLoop // Skips to the next iteration of the outer loop
        }
    }
}
```

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