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");- Repeating a block until a condition becomes false.
- 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.
- 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}");- Guaranteeing the loop body runs at least once, since the condition is checked afterward.
- 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.
- 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;
}
}- Using while (true) with an internal break condition for more complex exit logic.
- 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.
- 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