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
| Point | while loop | do-while loop |
|---|---|---|
| Condition checked | Before the body | After the body |
| Type | Entry-controlled | Exit-controlled |
| Minimum runs | 0 | 1 |
| Semicolon | No semicolon after while (cond) | Semicolon required: while (cond); |
| Syntax start | Starts with while | Starts with do |
| Best for | Loops that may not need to run at all | Menus, input validation, "run once then ask" |
| Example | Reverse digits while n != 0 | Show 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
- Both loops start with i = 10 and have the condition
i < 5, which is false from the start. - while: checks first, finds false, and never runs the body. Nothing is printed.
- do-while: runs the body first (prints 10), then checks
11 < 5, finds false, and stops. - 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 answer | Correct answer |
|---|---|
| while runs at least once | do-while runs at least once |
| do-while is entry-controlled | do-while is exit-controlled |
| They always give different output | Only when the condition is false at the start |
| Semicolon needed after while in a while loop | That creates an empty loop; only do-while needs it |
while vs do-while Practice Questions
- Write four differences between while and do-while loops.
- Which loop is exit-controlled?
- What is the output of
int x = 0; do { printf("A"); } while (x);? - What is the output of
int x = 0; while (x) { printf("A"); }? - When would you prefer do-while over while?
Show answers
- 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().
- do-while.
A(runs once, then x = 0 is false).- Nothing (x = 0 is false at the start).
- 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)
-
Which loop may not execute at all?
A) do-while B) while C) Both D) Neither
-
Which loop needs a semicolon after
while (condition)?A) while B) do-while C) Both D) for
-
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
-
"Look before you leap" describes:
A) do-while B) while C) goto D) switch
-
An entry-controlled loop is:
A) do-while B) while C) Both D) None
Show answers
- B: while.
- B: do-while.
- A: behave the same.
- B: while (check first).
- B: while (also for).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| while | Check first, entry-controlled, min 0 runs |
|---|---|
| do-while | Run first, exit-controlled, min 1 run |
| Semicolon after while() | Only in do-while |
| Different output when | Condition is false at the start |
| do-while best for | Menus, 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.