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 spec changed to "multiples of 3 only," you'd have to rewrite the whole thing.#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); }
for ( A ; B ; C ), each of the three semicolon-separated sections has its own distinct job.int i = 0i < 5i++for (int i = 0; i < 3; i++) printf("%d ", i);)printf("0 ") β C: i++ β i=1printf("1 ") β C: i++ β i=2printf("2 ") β C: i++ β i=3int i = 0; is declared outside the for.break.i += 1 at the end of the block.for (;;) is the classic infinite-loop idiom (equivalent to while (1)).for (int i = 0; i < N; i++) { ... }
for (int i = 1; i <= N; i++) { ... }
for (int i = N; i >= 0; i--) { ... }
for (int i = 0; i < N; i += 2) { ... }
for (;;))for (;;) {
...
if (exit_condition) break;
}
break β common for menu loops and event loops.| 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. A break inside a nested loop does not exit the outer one.continue means "skip the rest of this iteration and move to the next one." Unlike break, the loop doesn't exit. Let's trace it 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 ends the loop
continue, the for's update clause i++ still runs. That's why i=2 is skipped but i=3 comes next.
| break | continue | |
|---|---|---|
| Whole loop | Exits (done) | Continues (next iteration) |
| Rest of this iteration | Skipped | Skipped |
for's update i++ | Not run | Runs |
| When to use | "I'm done searching" | "skip just this element" |
continue inside 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 and continue each make sensedo-while and see 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.
for (int i=10; i>0; i--) execute?i takes the ten values 10, 9, 8, β¦, 1, so the body runs 10 times. The condition i > 0 means the body never runs when i=0.
for (;;) { ... } do?When all three parts (init / condition / update) are omitted, the missing condition is treated as always true, so the loop runs forever. Use break or return to exit.
for (int i = 0; i < 5; i++) { if (i == 3) continue; printf("%d ", i); }
continue skips the rest of the current iteration and moves on. The printf is only skipped when i=3, so the output is 0 1 2 4.
With break, the loop would exit entirely at that point, giving 0 1 2 β don't confuse the two.