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

# C++ While Loop

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

## Syntax

```cpp theme={"dark"}
while (condition) {
    // loop body
}
```

## Execution Mechanics

1. **Condition Evaluation:** The `condition` is evaluated first. This expression must be contextually convertible to `bool`.
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 jumps back to the condition evaluation.
4. **Termination:** If the condition evaluates to `false`, the loop terminates immediately. Control flow passes to the first statement following the loop block.

## Variable Declaration in Condition

C++ permits the declaration and initialization of a variable directly within the `while` condition. The loop executes as long as the initialized variable contextually converts to `true` (e.g., non-zero for integers, non-null for pointers).

```cpp theme={"dark"}
while (Type variable_name = expression) {
    // variable_name is in scope here
}
// variable_name is out of scope here
```

Variables declared this way are scoped strictly to the loop block and are destroyed and recreated on every iteration.

## Loop Control Statements

The standard execution flow of a `while` loop can be altered using jump statements:

* **`break`**: Immediately terminates the innermost enclosing `while` loop. Control flow jumps to the statement immediately following the loop body, bypassing any remaining condition checks.
* **`continue`**: Interrupts the current iteration. Control flow jumps directly to the end of the loop body, skipping any remaining statements, and forces an immediate re-evaluation of the `while` condition.

## Infinite Loops

If the condition expression never evaluates to `false` and no `break` statement is encountered, the loop becomes an infinite loop. The standard idiom for an intentional infinite loop in C++ is:

```cpp theme={"dark"}
while (true) {
    // executes indefinitely unless interrupted by a jump statement or program termination
}
```

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