Skip to main content

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.

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

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

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.
Master C# with Deep Grasping Methodology!Learn More