The Loop That Always Runs Once
A while loop tests before it acts, so it can run zero times. That is usually right. Sometimes it is exactly wrong: you cannot know whether to repeat a prompt until you have shown it, and you cannot judge a user's input until you have read some. do-while moves the test to the end, so the body runs first and the condition decides only whether to go again.
The condition i < 5 is false from the start. The while loop runs zero times; the do-while runs exactly one. That is the entire difference between them.
Syntax, and That Semicolon
do {
// body - always runs at least once
} while (condition);
The order reads the way it executes: do the body, while the condition still holds, do it again.
The trailing semicolon after the closing parenthesis is required. do ... while (cond); is one statement, and C statements end in semicolons. Leave it off and the compiler keeps reading, then reports a syntax error somewhere below - which is why the real mistake is often several lines from the line the error points at.
This is also the one place in C where while (cond); with a semicolon is correct rather than a bug. In a plain while loop the same punctuation creates an empty body and usually an infinite loop; here it is closing a do.
Braces are optional for a single-statement body but, as with every other C control structure, write them anyway.
The Input Validation Pattern
This is what do-while was designed for. You must read something before you can tell whether it is acceptable, so the read has to happen before the test:
The loop keeps reading until a value passes, and the same condition that rejects a bad value is the one that requests another pass. Written as a while, the read would have to appear twice - once before the loop to prime the condition, once at the end of the body - which is the duplication do-while removes.
With real input the body would call scanf instead:
int age;
do {
printf("Enter your age (1-120): ");
if (scanf("%d", &age) != 1) {
while (getchar() != '\n') { } /* discard the bad line */
age = -1; /* force another pass */
}
} while (age < 1 || age > 120);
Note the inner while that drains the rest of the line. If scanf fails to convert, the offending text is still sitting in the input buffer and every later read fails on it too - a loop that spins forever printing the prompt. Clearing it is part of the pattern.
The Menu Pattern
The other classic use: show the options, read a choice, act on it, and repeat until the user quits. The menu must appear before there is any choice to test.
break inside the switch ends the switch, not the loop - a point worth being precise about, since both use the same keyword. Here the loop ends because choice != 4 becomes false. If you did want to leave the loop from inside the switch, return or a flag variable would be the way; break alone cannot do it.
A Guaranteed First Pass Can Be a Bug
Because the body runs before any test, do-while will happily process data that does not exist:
/* BUG: prints a[0] even when n is 0 */
int i = 0;
do {
printf("%d\n", a[i]);
i++;
} while (i < n);
With n == 0 this reads a[0] on an empty array - undefined behavior. Any loop over a collection that might be empty wants a while or a for, whose test comes first.
The rule of thumb: use do-while only when a zero-iteration run is impossible by the nature of the problem, not merely unlikely in practice. Prompting a user, rolling dice until a target appears, generating and testing a candidate - those genuinely cannot be judged before the first pass. Walking a list cannot make that claim.
break and continue in a do-while
Both work, and continue has a twist worth knowing: it jumps to the condition test, not to the top of the body.
The i++ at the top of the body runs on every pass, including the ones continue cuts short, so the loop still advances. Put the update after a continue and the loop hangs - the same trap while loops have.
break leaves the loop immediately and skips the condition entirely.
Choosing Between the Three Loops
C's three loops are equally powerful; the choice is about which one states the intent.
for a counter, its limit, and its step belong together - "n times"
while repeat until a condition changes, and maybe not at all - "until"
do-while the same, but the first pass is guaranteed - "at least once"
In practice do-while is the rarest of the three by a wide margin. Most loops either count or need to handle the empty case, and both of those want the test at the top. Reach for do-while when you catch yourself duplicating a statement just before a while loop in order to prime its condition - that duplication is the signal.
Frequently Asked Questions
How do you write a do-while loop in C?
do { body } while (condition); - the body comes first, the test comes after, and the whole statement ends with a semicolon. The body always runs at least once, then repeats while the condition is nonzero.
What is the difference between while and do-while in C?
Where the test sits. A while loop checks the condition before the first pass, so it can run zero times. A do-while checks after, so it always runs at least once. Choose do-while only when that guaranteed first pass is genuinely what the problem needs.
Why does a do-while loop need a semicolon at the end?
Because do ... while (cond); is a single statement, and like every other C statement it is terminated by a semicolon. Leaving it off produces a confusing syntax error pointing at whatever line follows, since the compiler keeps reading for the rest of the statement.
When should I use a do-while loop?
When the body has to run before you can possibly know whether to repeat: showing a menu before reading a choice, re-prompting until the input is valid, or generating a value then testing it. If the condition can be checked up front, a while loop says it more plainly.