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

A `do-while` loop is a post-test control flow structure that guarantees the execution of a code block at least once before evaluating a boolean loop-continuation condition. If the condition evaluates to `true`, the loop iterates; if `false`, execution proceeds to the next statement following the loop.

## Syntax

```kotlin theme={"dark"}
do {
    // Statements to execute
} while (condition)
```

## Execution Mechanics

1. **Unconditional Execution:** The instruction pointer enters the `do` block and executes all statements sequentially. This occurs before any condition check.
2. **Condition Evaluation:** After the block executes, the boolean `condition` within the `while` clause is evaluated.
3. **Branching:**
   * If the condition evaluates to `true`, the instruction pointer jumps back to the start of the `do` block for the next iteration.
   * If the condition evaluates to `false`, the loop terminates, and control flow passes to the subsequent program statement.

## Variable Scoping

A distinct feature of Kotlin's `do-while` loop, contrasting with several other C-family languages, is its scoping rule. Variables declared locally within the `do` block remain in lexical scope and are fully accessible within the `while` condition evaluation.

```kotlin theme={"dark"}
fun main() {
    var countdown = 3
    
    do {
        val currentStatus = countdown--
        println("Status: $currentStatus")
    } while (currentStatus > 0) // 'currentStatus' is legally accessed here
}
```

## Structural Jump Expressions

The `do-while` loop supports Kotlin's structural jump expressions to alter control flow during an iteration. These jumps can be unlabelled (affecting the innermost enclosing loop) or labelled (affecting a specifically designated loop).

* **`break`**: Immediately terminates the innermost loop entirely, bypassing the `while` condition check, and transfers control to the statement following the loop.
* **`continue`**: Immediately halts the current iteration within the `do` block and jumps directly to the `while` condition evaluation of the innermost loop to determine if the next iteration should commence.
* **`break@label`**: Immediately terminates the specific loop marked with the matching label.
* **`continue@label`**: Immediately halts the current iteration and jumps to the `while` condition evaluation of the specific loop marked with the matching label.

```kotlin theme={"dark"}
fun main() {
    var x = 0
    
    outerLoop@ do {
        x++
        var y = 0
        
        do {
            y++
            
            // Halts current inner iteration, jumps to while (y < 3)
            if (y == 2) continue 
            
            // Terminates the outer loop entirely, bypassing all remaining iterations
            if (x == 3) break@outerLoop 
            
            println("x = $x, y = $y")
        } while (y < 3)
        
    } while (x < 5)
}
```

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