Loops driven by a condition rather than a counter. Pre-test while and post-test do-while.
while (cond) { ... } runs while the condition is truedo { ... } while (cond); runs at least oncedo-whilewhile (1) + break to exitwhile fits when the iteration count is not known in advance. "Keep going as long as this condition holds." In plain English: "while β¦ do β¦".while (condition) { // runs over and over while condition is true }
int n = 1; int count = 0; while (n <= 100) { n = n * 2; count++; } printf("%d steps to reach %d\n", count, n); // -> 7 steps to reach 128
while fits better than for. The loop repeats: check condition β body β check condition β β¦n <= 100{ ... } body}, go back to step 1for (int i = 0; i < 5; i++) { printf("%d\n", i); }
int i = 0; while (i < 5) { printf("%d\n", i); i++; }
for whenever the iteration count is fixed.| Situation | Best fit | Example |
|---|---|---|
| Known iteration count | for | walk over an array 0..N-1 |
| Stop on a condition (count unknown) | while | read until user types "q" |
| Must run at least once | do-while | prompt for input, re-prompt on invalid |
Infinite loop (exit via break) | while(1) / for(;;) | event / main loop |
while: forgetting to update the condition variable inside the body. for forces you to write the update in the header, so this class of bug is rarer.while checks the condition before the body (pre-test); do-while checks it after (post-test). That's why the do-while body always executes at least once.while (cond) { body; // 0 times if cond is false }
do { body; // always 1+ times } while (cond);
} while (cond); must end with ; or the compiler errors out. Unique to do-while.do-while fits perfectly.int choice; do { printf("Choose 1-3 > "); scanf("%d", &choice); if (choice < 1 || choice > 3) printf("Invalid input\n"); } while (choice < 1 || choice > 3); printf("You picked %d\n", choice);
int i = 10; while (i < 5) { printf("x"); }
int i = 10; do { printf("x"); } while (i < 5);
while / do-while is an infinite loop β forgetting to change the condition variable inside the body.int i = 0; while (i < 5) { printf("%d\n", i); // forgot i++! }
i never changes, so i < 5 is always true β an infinite loop printing 0 forever.
for makes this harder to miss because the update clause is part of the syntax.while (1) and break to leave.while (1) { // 1 is always true -> infinite int n; scanf("%d", &n); if (n == 0) break; // leave on 0 printf("Got: %d\n", n); }
break / return is the planned way out.int choice; do { printf("1-3 > "); // forgot scanf! choice stays uninitialized } while (choice < 1 || choice > 3);
choice never changes, so depending on its initial value the loop can run forever. Make sure the variable the condition tests is updated every iteration.
Ctrl + C stops a terminal program. In Visual Studio, use the "Stop Debugging" button.for and verify same outputn = n * 3 and see the step countdo-whilewhile(1) + break to read numbers until 0Check your understanding.
while(1) do?The condition is always 1 (true), so it loops forever unless a break or return exits the loop.
do-while and while?do-while checks the condition after the body, so the body always runs at least once. while checks first, so the body can run zero times.
int i = 10; while (i < 5) { printf("x"); i++; }
while is pre-test. i=10 fails i<5 immediately, so the body runs zero times.
do-while?int i = 10; do { printf("x"); i++; } while (i < 5);
do-while is post-test: run once, then check. After the first iteration i=11, and 11 < 5 is false, so the loop ends. One iteration β the classic one-more-than-while behaviour.
int i = 0; while (i < 5) { printf("%d\n", i); }
i++ is missing. i stays 0 and the condition never becomes false — infinite loop printing 0. The most common while bug. Ctrl+C to stop.
do-while?{};} while (cond); requires the trailing ; or the compiler rejects it. A do-while-only rule.