TExcelTAD
Home › C# › C# While Loop

C# While Loop

Loops repeat a block of code. A while loop keeps going as long as its condition is true. Use it when you do not know in advance how many repetitions you need — for example, "keep reading until the user types quit" or "keep halving until the value is small enough".

The while loop

The condition is checked before each iteration. If it is false from the start, the body never runs.

csharp · try it yourself
using System;

int countdown = 5;

while (countdown > 0)
{
    Console.Write(countdown + " ");
    countdown--;
}
Console.WriteLine("Liftoff!");
5 4 3 2 1 Liftoff!

Something inside the loop must eventually make the condition false — here, countdown--. Forget it and you have an infinite loop.

A loop with an unknown number of steps

How many years does it take for savings to double at 7% interest? We cannot easily tell in advance, so while is a natural fit:

csharp · try it yourself
using System;

decimal balance = 1000m;
int years = 0;

while (balance < 2000m)
{
    balance *= 1.07m;
    years++;
}

Console.WriteLine(
quot;Doubled after {years} years: {balance:F2}");
Doubled after 11 years: 2104.85

Another classic: count the digits of a number by repeatedly dividing by 10.

csharp · try it yourself
using System;

int number = 907153;
int digits = 0;
int sum = 0;

while (number > 0)
{
    sum += number % 10;   // last digit
    number /= 10;         // drop the last digit
    digits++;
}

Console.WriteLine(
quot;Digits: {digits}, digit sum: {sum}");
Digits: 6, digit sum: 25

The do..while loop

A do..while loop checks its condition after the body, so the body always runs at least once. This suits menus and input prompts, where you must show something before you can decide whether to repeat.

csharp · try it yourself
using System;

int value = 100;

do
{
    Console.WriteLine(
quot;value = {value}"); value *= 2; } while (value < 50); Console.WriteLine("The body ran once even though 100 < 50 is false.");
value = 100
The body ran once even though 100 < 50 is false.

Note the semicolon after while (...) in a do..while — it is required.

Here is a menu loop driven by a fixed list of simulated choices:

csharp · try it yourself
using System;

string[] choices = { "1", "2", "9", "0" };
int i = 0;
string choice;

do
{
    choice = choices[i++];
    switch (choice)
    {
        case "1": Console.WriteLine("Showing balance"); break;
        case "2": Console.WriteLine("Making a deposit"); break;
        case "0": Console.WriteLine("Goodbye"); break;
        default:  Console.WriteLine(
quot;Unknown option {choice}"); break; } } while (choice != "0");
Showing balance
Making a deposit
Unknown option 9
Goodbye

while versus do..while

while do..while
Condition checked Before each iteration After each iteration
Minimum number of runs 0 1
Typical use Processing while data is available Menus, "try again" prompts

Tip: while (true) { … } with a break inside is a common, perfectly fine pattern when the exit condition is easiest to test in the middle of the body.

Test yourself

How many times does the body run? int n = 0; while (n < 3) { n++; }

  1. 2
  2. 3
  3. 4
  4. Forever

Which loop always executes its body at least once?

  1. while
  2. for
  3. do..while
  4. foreach