While & Do-While Loops in C
A while loop in C repeats a block of code as long as a condition stays true, and it checks that condition before each pass. A do-while loop does the same job but checks the condition after the body runs, so the body always executes at least once. That single difference — test-first versus test-last — is the whole story.
The while loop
The while loop is the simplest counting and condition-driven loop in C. Its shape is:
while (condition) {
/* body runs while condition is true */
}
C evaluates condition. If it is non-zero (true), the body runs, then C re-checks the condition. The moment the condition becomes zero (false), the loop stops.
#include <stdio.h>
int main(void) {
int count = 1; /* start the counter */
while (count <= 5) { /* condition checked BEFORE each pass */
printf("%d ", count); /* prints the current value */
count++; /* move toward the stopping condition */
}
printf("\nDone.\n");
return 0;
}
Output: 1 2 3 4 5 then Done. If you forget count++, the condition never becomes false and you get an infinite loop.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingThe do-while loop
A do-while loop puts the body first and the test at the bottom:
do {
/* body runs at least once */
} while (condition); /* note the semicolon here */
Because the test comes last, the body runs once before the condition is ever checked. This is exactly what you want for input validation or menus — you must show the menu at least once.
#include <stdio.h>
int main(void) {
int number;
do {
printf("Enter a positive number: ");
if (scanf("%d", &number) != 1) { /* guard against bad input */
return 1;
}
} while (number <= 0); /* repeat until a positive value is given */
printf("You entered: %d\n", number);
return 0;
}
Even if the very first value is valid, the prompt still appeared once — that is the do-while guarantee.
When to choose which
- Use while when the body might need to run zero times (e.g. processing a list that could be empty).
- Use do-while when the body must run at least once (menus, retry-until-valid input).
Both are interchangeable in many cases; pick the one that makes your intent obvious. If you are counting a known number of times, a for loop is usually cleaner.
Common mistakes
- Putting a semicolon after
while (...)in a normal while loop:while (x < 5);creates an empty body and loops forever. The semicolon belongs only at the end of a do-while. - Forgetting to update the loop variable, so the condition never changes — an infinite loop.
- Off-by-one errors:
<=versus<changes how many times the loop runs. Trace the first and last passes by hand. - Confusing
=and==:while (x = 5)assigns 5 (always true) instead of comparing. Use==for comparison. - Reading input without checking
scanf's return value, which lets bad input slip through.
FAQ
Is do-while used much in C? Less than while and for, but it shines for input validation and menu loops where one guaranteed pass is natural.
Can I break out of a while loop early? Yes — break; exits immediately, and continue; skips to the next condition check.
Related reading: the hub at C Programming, For Loops in C, and Operators in C.
Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in C
A beginner guide to arrays in C — declaring, initializing and indexing them, looping over elements, multidimensional arrays, and avoiding out-of-bounds errors.
Common C Errors & How to Fix Them
A troubleshooting guide to the most common C errors beginners face — from = vs ==, format mismatches and missing semicolons to segmentation faults — and how to fix each.
Dynamic Memory Allocation in C
A beginner guide to dynamic memory allocation in C — malloc, calloc, realloc and free, allocating at runtime, and preventing memory leaks.
