Learn how to use for and while loops in C with step-by-step visual examples.
for (init; cond; update) { ... }while (cond) { ... } runs while cond is truebreak exits; continue skips to next iterationdo-while runs at least oncefor (;;) / while (1)#include <stdio.h> int main(void) { printf("1\n"); printf("2\n"); printf("3\n"); printf("4\n"); printf("5\n"); return 0; }
printf("1\n"); printf("2\n"); ... printf("100\n"); for 100 lines? And if the requirement changed to "print only multiples of 3", you'd have to rewrite everything.#include <stdio.h> int main(void) { for (int i = 1; i <= 5; i++) { printf("%d\n", i); } return 0; }
5 to 100. Even 10,000 iterations fit in the same amount of code.// Without loop: manually add each number int sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10; // With loop int sum = 0; for (int i = 1; i <= 10; i++) sum += i;
for (int i = 0; i < 5; i++) { printf("i = %d\n", i); }
| Name | Type | Value |
|---|
for (int i=0; i<10; i++) { if (i == 5) break; printf("%d ", i); } // Output: 0 1 2 3 4
for (int i=0; i<5; i++) { if (i == 2) continue; printf("%d ", i); } // Output: 0 1 3 4 (skips 2)
break and continue only affect the innermost loop. break inside a nested loop does not exit the outer loop.continue means "skip the rest of this iteration, go to the next one". Unlike break, the loop does not exit. Let's trace the execution step by step.for (int i = 0; i < 5; i++) { if (i == 2) continue; // β when i==2, skip the rest printf("%d ", i); // not reached after continue }
0 β i++ makes i=11 β i++ makes i=23 4 β i=5 terminates
continue, the for's update clause i++ is still executed. That's why i=2 skipped but i=3 follows.
| break | continue | |
|---|---|---|
| Whole loop | Exit (done) | Continue (next iteration) |
| Rest of this iteration | Skip | Skip |
for's update i++ | Not executed | Executed |
| When to use | "done searching" | "skip just this element" |
continue in a while can cause an infinite loopcontinue can skip it. That's a classic infinite-loop trap.
int i = 0; while (i < 5) { if (i == 2) continue; // β never reaches i++ β stuck at i=2 forever printf("%d ", i); i++; }
if (i == 2) { i++; continue; } // β safe
continue or with an if. Which reads better depends on the situation.
for (int i=0; i<n; i++) { if (arr[i] < 0) continue; // skip negatives if (arr[i] == 0) continue; // skip zeros too // long main processing for positives... process(arr[i]); total += arr[i] * 2; }
for (int i=0; i<n; i++) { if (arr[i] > 0) { // invert & wrap process(arr[i]); total += arr[i] * 2; } }
ifcontinue (guard-clause style)return in functions β see if Statement.
break vs continue appropriatelydo-while and observe the guaranteed-once behaviorCheck your understanding of this lesson!
for (int i=0; i<5; i++) execute?i takes the five values 0, 1, 2, 3, 4, so the body runs 5 times.
while(1)?The condition is always 1 (true), so the loop keeps running forever unless broken out of by break or return.
do-while tests the condition at the end of the loop, so the body always runs at least once. while tests first, so it can run zero times.
Combine this interactive site with books for more practice.
* Links above are affiliate links. Purchases help support this site.