> ## 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 in C# is an entry-controlled iteration statement that repeatedly executes a block of code as long as a specified boolean expression evaluates to `true`. Because the condition is evaluated prior to the execution of the loop's body, it is classified as a pre-test loop, meaning the enclosed statements may execute zero or more times.

## Syntax

```csharp theme={"dark"}
while (boolean_expression)
{
    // Statements to execute
}
```

## Execution Mechanics

1. **Condition Evaluation:** Control flow reaches the `while` statement and evaluates the `boolean_expression`. This expression must resolve to a `bool` data type.
2. **Execution:** If the expression evaluates to `true`, the runtime enters the loop block and executes the statements sequentially.
3. **Iteration:** Upon reaching the end of the block, control jumps back to the top of the loop to re-evaluate the `boolean_expression`.
4. **Termination:** If the expression evaluates to `false` at any point during the condition check, the loop terminates immediately. Control is transferred to the first statement following the loop block.

## Code Example

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

while (counter < 3)
{
    Console.WriteLine(counter);
    counter++; // Modifies the state evaluated in the condition
}
```

## Control Transfer Statements

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

* `break`: Immediately terminates the entire loop. Control passes to the statement directly following the loop block, regardless of the boolean expression's state.
* `continue`: Immediately terminates the current iteration. Control jumps directly back to the `boolean_expression` evaluation, bypassing any remaining statements in the loop body for that specific iteration.

## Technical Considerations

* **Infinite Loops:** If the `boolean_expression` is statically `true` (e.g., `while (true)`) or if the state evaluated by the condition is never modified within the loop body, the loop will execute indefinitely until the process is terminated or a `break` statement is encountered.
* **Variable Scope:** Variables declared within the `while` loop block are scoped locally to that block. They are created and destroyed on each iteration. Variables evaluated in the `boolean_expression` must be declared in an outer scope prior to the loop.
* **Single-Statement Body:** If the loop body consists of only a single statement, the curly braces `{ }` can be omitted, though retaining them is a standard convention for maintainability.

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