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.

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");

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: ");
    number = int.Parse(Console.ReadLine() ?? "0");
} while (number <= 0);

Console.WriteLine($"You entered: {number}");

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

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

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Variables & Data Types
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.
Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.
Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.
Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.