Skip to content
StudyFame

while vs do-while in C

"Write the difference between while and do-while loops" is one of the most common short questions in C exams. Both repeat code while a condition is true, so what's different?

Just one thing, really: when the condition is checked. But that one difference has important effects. This lesson explains it with a table and a program you can run.

What You Will Learn

  • The key difference between while and do-while.
  • A complete comparison table for exams.
  • A program that shows the difference in output.
  • When to choose each loop.

while vs do-while in C

Exam definition: In a while loop, the condition is checked before the body (entry-controlled), so the body may run zero times. In a do-while loop, the condition is checked after the body (exit-controlled), so the body runs at least once.

Difference between while and do-while loop

Pointwhile loopdo-while loop
Condition checkedBefore the bodyAfter the body
TypeEntry-controlledExit-controlled
Minimum runs01
SemicolonNo semicolon after while (cond)Semicolon required: while (cond);
Syntax startStarts with whileStarts with do
Best forLoops that may not need to run at allMenus, input validation, "run once then ask"
ExampleReverse digits while n != 0Show menu until user picks Exit

Memory trick: while = "Look before you leap" (check, then act). do-while = "Leap, then look" (act, then check).

Syntax Side by Side

/* while */                  /* do-while */
while (condition)             do
{                             {
    body;                         body;
}                             } while (condition);

Example 1: Same Condition, Different Output

#include <stdio.h>

int main(void)
{
    int i = 10;

    printf("while   : ");
    while (i < 5)
    {
        printf("%d ", i);
        i++;
    }
    printf("(end)\n");

    i = 10;
    printf("do-while: ");
    do
    {
        printf("%d ", i);
        i++;
    } while (i < 5);
    printf("(end)\n");

    return 0;
}

Output

while   : (end)
do-while: 10 (end)

Code Explanation

  1. Both loops start with i = 10 and have the condition i < 5, which is false from the start.
  2. while: checks first, finds false, and never runs the body. Nothing is printed.
  3. do-while: runs the body first (prints 10), then checks 11 < 5, finds false, and stops.
  4. So when the condition starts false, the do-while runs exactly once and the while runs zero times.

How They Behave When the Condition Is True

  • If the condition is true at the start, both loops behave the same way and run the same number of times.
  • The difference only shows up when the condition is false at the very first check.
  • Any do-while can be rewritten as a while (by copying the body once before the loop), and any while can be rewritten as a do-while (by wrapping it in an if). Pick whichever is clearer.

Example 2: Same Job, Both Loops (Print 1 to 3)

#include <stdio.h>

int main(void)
{
    int a = 1, b = 1;

    while (a <= 3)
    {
        printf("while %d\n", a);
        a++;
    }

    do
    {
        printf("do-while %d\n", b);
        b++;
    } while (b <= 3);

    return 0;
}

Output

while 1
while 2
while 3
do-while 1
do-while 2
do-while 3

With a true starting condition, both loops run 3 times.

Important Points to Remember

  • while checks before, do-while checks after.
  • while: entry-controlled, minimum 0 runs.
  • do-while: exit-controlled, minimum 1 run.
  • Only do-while needs a semicolon after while (...).
  • They differ only when the condition is false at the start.

Common Mistakes Students Make

Wrong answerCorrect answer
while runs at least oncedo-while runs at least once
do-while is entry-controlleddo-while is exit-controlled
They always give different outputOnly when the condition is false at the start
Semicolon needed after while in a while loopThat creates an empty loop; only do-while needs it

while vs do-while Practice Questions

  1. Write four differences between while and do-while loops.
  2. Which loop is exit-controlled?
  3. What is the output of int x = 0; do { printf("A"); } while (x);?
  4. What is the output of int x = 0; while (x) { printf("A"); }?
  5. When would you prefer do-while over while?
Show answers
  1. while checks before, do-while after; while is entry-controlled, do-while exit-controlled; while may run 0 times, do-while at least once; do-while needs a semicolon after while().
  2. do-while.
  3. A (runs once, then x = 0 is false).
  4. Nothing (x = 0 is false at the start).
  5. When the body must run at least once, such as a menu or an input check.

Coding Exercise

Write a program that asks "Do you want to see a fact? (y/n)" and prints a C fact each time the user types y. It must ask at least once. Which loop fits?

Show solution
#include <stdio.h>

int main(void)
{
    char answer;
    int count = 0;

    do
    {
        printf("Do you want to see a fact? (y/n): ");
        scanf(" %c", &answer);
        if (answer == 'y')
        {
            count++;
            printf("Fact %d: C was created in 1972.\n", count);
        }
    } while (answer == 'y');

    printf("Bye!\n");
    return 0;
}
Do you want to see a fact? (y/n): y
Fact 1: C was created in 1972.
Do you want to see a fact? (y/n): y
Fact 2: C was created in 1972.
Do you want to see a fact? (y/n): n
Bye!

do-while fits, because the question must be asked at least once.

Quick Quiz: while vs do-while (MCQ)

  1. Which loop may not execute at all?

    A) do-while   B) while   C) Both   D) Neither

  2. Which loop needs a semicolon after while (condition)?

    A) while   B) do-while   C) Both   D) for

  3. If the condition is true at the start, while and do-while:

    A) Behave the same   B) Always differ   C) Both fail   D) Run once

  4. "Look before you leap" describes:

    A) do-while   B) while   C) goto   D) switch

  5. An entry-controlled loop is:

    A) do-while   B) while   C) Both   D) None

Show answers
  1. B: while.
  2. B: do-while.
  3. A: behave the same.
  4. B: while (check first).
  5. B: while (also for).

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

whileCheck first, entry-controlled, min 0 runs
do-whileRun first, exit-controlled, min 1 run
Semicolon after while()Only in do-while
Different output whenCondition is false at the start
do-while best forMenus, input validation

Frequently asked questions

What is the main difference between while and do-while?
while checks the condition before the body; do-while checks it after, so it runs at least once.
Which is entry-controlled and which is exit-controlled?
while is entry-controlled; do-while is exit-controlled.
Can while and do-while give the same output?
Yes, whenever the condition is true at the start.

Remember

  • while = look before you leap; do-while = leap, then look.
  • Min runs: while 0, do-while 1.

Next Topic

Next lesson: Nested Loops in C. You will learn to put one loop inside another.