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

# Dart While Loop

A `while` loop in Dart is a pre-test control flow statement that repeatedly executes a block of code as long as a specified boolean condition evaluates to `true`. Because the condition is evaluated before the loop body executes, the code block will not execute at all if the initial condition is `false`.

## Syntax

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

## Execution Mechanics

1. **Condition Evaluation:** The loop begins by evaluating the `condition` expression. In Dart, this expression must evaluate strictly to a boolean (`bool`) type. Dart does not support implicit truthy/falsy type coercion (e.g., `while (1)` will result in a compile-time error).
2. **Execution:** If the condition evaluates to `true`, the statements within the loop body are executed sequentially.
3. **Iteration:** After the loop body completes, control flow returns to the top of the loop, and the condition is re-evaluated.
4. **Termination:** The loop terminates immediately when the condition evaluates to `false`. Control flow then passes to the first statement following the loop block.

## Code Example

```dart theme={"dark"}
int counter = 0;

while (counter < 3) {
  print(counter);
  counter++; // State mutation to ensure eventual termination
}
```

## Control Transfer Statements

You can alter the standard execution flow of a `while` loop using control transfer statements:

* **`break`**: Immediately terminates the loop entirely, bypassing the condition check and transferring execution to the code following the loop block.
* **`continue`**: Halts the current iteration, skips any remaining statements in the loop body, and immediately jumps back to re-evaluate the loop condition.

```dart theme={"dark"}
int i = 0;

while (i < 5) {
  i++;
  if (i == 2) {
    continue; // Skips the rest of the block, re-evaluates (i < 5)
  }
  if (i == 4) {
    break; // Exits the loop entirely
  }
  print(i);
}
```

## Infinite Loops

If the loop's condition never evaluates to `false`—typically due to a lack of state mutation within the loop body—it results in an infinite loop. This will block the current isolate's thread of execution indefinitely.

```dart theme={"dark"}
// Example of an infinite loop
while (true) {
  // This block will execute forever unless a 'break' statement is encountered
}
```

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