Codectionary / Developer documentation / C#

while and do-while Loops

A while loop repeats its block as long as its condition remains true, checking the condition before each iteration - so the body might never execute at all. A do-while loop is similar, but checks the condition after the body runs, guaranteeing the loop body executes at least once. Both are ideal when the number of iterations isn't known ahead of time.

Read the syntax

while checks a bool condition before each iteration, so its body might run zero times. do ... while checks after the body and therefore runs at least once; its trailing semicolon belongs to the syntax.

Every repeating loop needs a route to termination, such as updating the condition’s variable or using break. Parse can throw on invalid input, so TryParse is safer for an interactive prompt.

Syntax

while (condition) {\n  // code\n}

Examples

Basic while Loop

Repeating a block until a condition becomes false.

int count = 0;

while (count < 5)
{
    Console.WriteLine(count);
    count++;
}

Console.WriteLine("Done");
  1. Repeating a block until a condition becomes false.
  2. while checks a bool condition before each iteration, so its body might run zero times. do ... while checks after the body and therefore runs at least once; its trailing semicolon belongs to the syntax.
  3. count begins at 0 and increases once per loop, printing 0 through 4. When count becomes 5, the condition is false and Done prints.

do-while Loop

Guaranteeing the loop body runs at least once, since the condition is checked afterward.

int number;

do
{
    Console.Write("Enter a positive number: ");
    if (!int.TryParse(Console.ReadLine(), out number)) number = 0;
} while (number <= 0);

Console.WriteLine($"You entered: {number}");
  1. Guaranteeing the loop body runs at least once, since the condition is checked afterward.
  2. while checks a bool condition before each iteration, so its body might run zero times. do ... while checks after the body and therefore runs at least once; its trailing semicolon belongs to the syntax.
  3. The prompt appears at least once. TryParse treats nonnumeric input as a failed conversion, so the loop asks again until a positive integer is entered and echoed.

while Loop with break

Using while (true) with an internal break condition for more complex exit logic.

int attempts = 0;

while (true)
{
    attempts++;
    if (attempts == 3)
    {
        Console.WriteLine($"Success after {attempts} attempts");
        break;
    }
}
  1. Using while (true) with an internal break condition for more complex exit logic.
  2. while checks a bool condition before each iteration, so its body might run zero times. do ... while checks after the body and therefore runs at least once; its trailing semicolon belongs to the syntax.
  3. The true condition would continue indefinitely without break. attempts increases to 3, prints the success message, then break exits the loop.

Best practices

  • Use a do-while loop specifically when the loop body must run at least once, such as showing a menu before checking for an exit condition
  • Always ensure the while condition will eventually become false, or include a break, to avoid an infinite loop
  • Prefer a for loop when the number of iterations is known in advance - reserve while for genuinely condition-driven repetition
  • Double-check that variables used in the loop condition are actually being updated somewhere inside the loop body

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading