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 is the right fit when you don't know the iteration count up front. The idea is "keep going as long as this condition holds" β just like the English word "while."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 β run body β check condition β β¦n <= 100{ ... } body}, jump 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 | walking an array 0..N-1 |
| Stop on a condition (count unknown) | while | reading until the user types "q" |
| Must run at least once | do-while | prompt for input, re-prompt if invalid |
Infinite loop (exit via break) | while(1) / for(;;) | an event or main loop |
while mistake: forgetting to update the condition variable inside the body. for forces you to put the update in the header, so this class of bug is rarer there.while checks the condition before the body (pre-test); do-while checks it after the body (post-test). That's why a do-while body always runs at least once.while (cond) { body; // 0 times if cond is false }
do { body; // always 1+ times } while (cond);
} while (cond); has to end with ; or the compiler will reject it. This quirk is unique to do-while.do-while is a natural fit.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 bug 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 stays true β an infinite loop that prints 0 forever.
for makes this harder to miss because the update clause is baked into the syntax.while (1) with break to get out.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 or return is the planned exit.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 may run forever. Make sure every iteration updates the variable the condition tests.
Ctrl + C to stop a terminal program. In Visual Studio, click "Stop Debugging."for and confirm you get the same outputn = n * 3 and watch the step countdo-whilewhile(1) + break to read numbers until you see 0Check your understanding.
while(1) do?The condition is always 1 (true), so it loops forever unless a break or return takes you out.
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 may run zero times.
int i = 10; while (i < 5) { printf("x"); i++; }
while is pre-test. With i=10, i<5 is false right away, 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" behavior.
int i = 0; while (i < 5) { printf("%d\n", i); }
i++ is missing. i stays at 0 and the condition never becomes false — an infinite loop that prints 0. The most common while bug. Ctrl+C to stop.
do-while?{};} while (cond); needs the trailing ; or the compiler rejects it. This rule is unique to do-while.