Skip to content
Theory · Conditions and loops · lesson 6 of 13

while and do-while

Picture a runner at a stadium. The coach's order is to keep running laps until it gets dark. Before each new lap, the runner passes under the arch at the start and looks at the sky: still light—another lap; dark—off the track. Nobody knows in advance how many laps there will be.

A program often needs to repeat the same action too. Repetition is the job of a loop, a construct that runs its code again and again while a condition is true. Let's start with the while loop. The program below counts how many times the number 100 can be halved until 1 is left:

C#
int n = 100;
int steps = 0;

while (n > 1)
{
    n /= 2;
    steps++;
}

Console.WriteLine(steps); // 6

Let's see what happens:

  1. int n = 100; and int steps = 0; set up the number and a counter of divisions.
  2. while (n > 1) checks the condition: 100 > 1 is true, so the code in braces runs—the loop body.
  3. The body halves n and adds one to steps.
  4. Then the loop goes back to the condition and checks it again, now with the new n.
  5. The values of n go 100, 50, 25, 12, 6, 3, 1. At 1 the condition n > 1 is false, and the loop ends.
  6. Console.WriteLine(steps) stands after the loop and prints 6: that is how many divisions there were.

One pass through the loop body is called an iteration. This program has six iterations, one per division.

How while works

A while loop repeats two steps:

  1. Check the condition.
  2. If it is true, run the body and go back to step 1. If it is false, move on to the code after the loop.

The condition is checked before the very first iteration as well. If it is false from the start, the body never runs: with n = 1, the program prints 0.

while is the choice when the number of repetitions is not known in advance and depends on what happens in the body. The runner doesn't know how many laps fit before dark either.

A loop that never ends

If the body does not change the variable in the condition, the condition stays true, and the loop never ends. Remove the line n /= 2; from the example, and n stays 100 forever. The program keeps spinning the loop until you stop it: in the terminal, press Ctrl+C.

This is where the stadium picture is off. A runner gets tired sooner or later and leaves the track, but a loop that never changes its variable spins forever.

The do-while loop

while has a relative: the do loop. It is written like this: do { body } while (condition);. It checks the condition after the body, so the body runs at least once. The runner first runs a lap and only then looks at the sky.

The do loop suits counting the digits of a number. The number 0 has one digit, and the loop must account for that:

C#
int n = 0;
int digits = 0;

do
{
    digits++;
    n /= 10;
}
while (n > 0);

Console.WriteLine(digits); // 1

Let's go step by step:

  1. The body runs at once, with no check: digits becomes 1, and n /= 10 leaves 0 in n.
  2. Only now is the condition n > 0 checked. It is false, and the loop ends.
  3. The program prints 1: the number 0 has one digit.

With a plain while, the condition n > 0 would be checked before the body. The body would never run, and the program would print the wrong answer 0. For n = 4096, both loops give 4: they differ only when the condition is false from the very start.

Note the syntax: in a do loop, while (n > 0) is followed by a semicolon, while a plain while has none.

Interviewers often ask how the while loop differs from the do loop. The difference is when the condition is checked: while checks it before the body, and do after it, so the body of do runs at least once.

Common mistake

The braces around the loop body are left out, and both lines of the body are lined up with indentation:

C#
int n = 100;
int steps = 0;

while (n > 1)
    n /= 2;
    steps++; // ✗

Console.WriteLine(steps);

The program compiles with no errors or warnings, but prints 1 instead of 6.

Without braces, the loop body is a single statement, n /= 2;. The compiler ignores indentation, so steps++; already stands after the loop and runs once. Put the braces back around both lines, as in the first example.

Key points

  • while checks the condition before every iteration and runs the body while the condition is true.
  • The body must change the variable in the condition, or the loop never ends.
  • The do loop checks the condition after the body, so the body runs at least once.
  • Without braces, the loop body is only the first statement after the condition, whatever the indentation.