A while loop tests its condition before each iteration, so its body may run zero times. A do while loop runs the body first and tests the condition afterwards, so its body always runs at least once. That is the whole difference between a while and do while loop in C, and it decides which one fits a given task.
Both repeat code while a condition is non-zero. This guide compares their syntax, shows when each is the right tool and points out the semicolon rule that trips up beginners.
Note the semicolon after the closing parenthesis of do while. It is required there and wrong after a plain while (condition), where it would create an empty loop body.
With a starting value that satisfies the condition, both loops give the same output. The difference shows when the condition is false from the start.
A while loop is entry-controlled: the condition is checked before the body. A do while loop is exit-controlled: it is checked after the body.
The while loop never runs, because x > 0 is false immediately. The do while loop prints once before it checks the condition and stops.
| Feature | while | do while |
|---|---|---|
| Condition checked | Before the body | After the body |
| Minimum executions | 0 | 1 |
| Semicolon after condition | No | Yes |
| Typical use | Unknown number of repeats, may be none | Prompts, menus, retry until valid |
Use while when the loop may legitimately do nothing, or when you read until a condition ends the data:
getchar returns an int, not a char, so that EOF can be told apart from every valid character. The loop copies standard input to standard output and ends at end of file, possibly without a single pass if the input is empty.
Another common case is processing a number digit by digit:
If n were 0, the loop would correctly add nothing.
Use do while when the action must happen once before you can decide whether to repeat. The classic example is input validation or a menu:
The prompt must appear at least once, and choice has no meaningful value until the user answers, so testing after the body is natural. The inner while discards the rest of a bad line so the loop does not spin forever on non-numeric input. Checking the return value of scanf is important: it returns the number of items read.
The do while form is also used inside multi-statement macros, written as do { ... } while (0), so the macro behaves like one statement and works safely in an if without braces.
Forgetting to change the variable in the condition produces an infinite loop:
Other frequent errors:
while (i < 5);, leaves an empty body and then (when i is never changed) hangs.do { } while (cond) is a compile error.= instead of == in the condition assigns a value, and the compiler usually warns with -Wall.An intentional infinite loop is written while (1) or for (;;) and left with break or return.
Only do while. A while loop skips its body entirely if the condition is false at the start.
Yes, by adding an if check before the loop, though it is rarely clearer. Choose the form that matches how the logic reads.
Less often than for and while, but it is the natural fit for menus, prompts and retry logic where one attempt is always needed.
They are optional for a single statement, but always using braces avoids bugs when a second line is added later.
A while loop checks its condition first and may run zero times; a do while loop checks afterwards and always runs once. Remember the semicolon after do while (condition), make sure something in the body changes the condition, and use do while for prompts and retries where the first pass must happen before a decision.