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

A `do-while` loop in C# is a post-test iteration statement that executes a specified block of code exactly once before evaluating a boolean condition to determine whether the loop should repeat. Because the condition is evaluated at the end of the loop's execution path, the enclosed statements are guaranteed to execute at least once, regardless of the condition's initial state.

## Syntax

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

**Syntactic Rules:**

* The `do` keyword initiates the block.
* The loop body is enclosed in curly braces `{}`. Braces are optional if the body contains only a single statement, but their use is a standard best practice to prevent logic errors (such as accidentally excluding subsequent statements from the loop body when modifying code). Furthermore, C# explicitly forbids variable declarations as the single statement of an unbraced loop.
* The `while` keyword follows the closing brace and contains the condition enclosed in parentheses `()`.
* The `boolean_expression` must be of type `bool`, or be implicitly convertible to `bool` (or belong to a type that overloads the `true` and `false` operators). C# does not allow integer-to-boolean implicit conversions (e.g., `while (1)` is invalid).
* A terminating semicolon `;` is strictly required at the end of the `while` statement.

## Scoping Rules

A critical scoping rule unique to `do-while` loops is that variables declared inside the `do` block are scoped locally to that block. They are out of scope and cannot be accessed within the `while` condition.

Attempting to evaluate a variable declared inside the `do` block will result in a compiler error:

```csharp theme={"dark"}
// INVALID: Will not compile (CS0103: The name 'input' does not exist in the current context)
do 
{ 
    string input = Console.ReadLine(); 
} while (input != "quit"); 
```

To evaluate a variable in the `while` condition, it must be declared in the outer scope before the `do` block begins:

```csharp theme={"dark"}
// VALID
string input;
do 
{ 
    input = Console.ReadLine(); 
} while (input != "quit");
```

## Execution Mechanics

1. **Execution:** The runtime enters the `do` block and executes all statements sequentially.
2. **Evaluation:** Upon reaching the end of the block, the runtime evaluates the `boolean_expression`.
3. **Branching:**
   * If the expression evaluates to `true`, control flow jumps back to the beginning of the `do` block for the next iteration.
   * If the expression evaluates to `false`, the loop terminates, and control flow passes to the next sequential statement in the program following the `do-while` construct.

## Code Example

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

do
{
    Console.WriteLine($"Iteration: {iterationCount}");
    iterationCount++;
} while (iterationCount < 3);
```

**Execution Trace:**

* **Pass 1:** `iterationCount` is 0. Prints "Iteration: 0". Increments to 1. Evaluates `1 < 3` (`true`).
* **Pass 2:** `iterationCount` is 1. Prints "Iteration: 1". Increments to 2. Evaluates `2 < 3` (`true`).
* **Pass 3:** `iterationCount` is 2. Prints "Iteration: 2". Increments to 3. Evaluates `3 < 3` (`false`). Loop terminates.

## Control Transfer Statements

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

* **`break`**: Immediately terminates the loop. Control flow jumps to the statement immediately following the `while (condition);`. The condition is not evaluated.
* **`continue`**: Immediately halts the current iteration's execution block. Control flow jumps directly to the `while (condition);` evaluation to determine if the next iteration should begin.
* **`return` / `throw`**: Exits the loop by either returning from the enclosing method or propagating an exception up the call stack.

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