C

Loops

A loop repeats a block of code while some condition holds — the tool for "do this 10 times," "keep reading until there's no more input," or "process every element in this array." C has three loop forms; this chapter is about picking the right one, not just knowing the syntax.

Quick reference
What it is
Three ways to repeat a block of code: for, while, and do-while.
Mental model
All three ask the same question — "should I run this block again?" — they only differ in when that question gets asked and how the loop's own bookkeeping is written.
Key numbers
A do-while body always runs at least once; for and while can run zero times if the condition starts false.
Reach for it when
for when you know the number of iterations up front; while when you don't; do-while when the body must run at least once regardless.

for

A for loop bundles the three parts of "repeat N times" into one line: an initializer, a condition, and an update.

for (int i = 0; i < 5; i++) {
    printf("%d\n", i);
}
// 0 1 2 3 4
The for loop initializes i once, then repeatedly checks the condition, runs the body, and applies the update, looping back until the condition is false init: i = 0 i < 5? yes run body update: i++ no loop ends
The initializer runs exactly once, but the condition and update repeat every pass — this is the entire mechanism, whichever loop form you write it in.

while

A while loop checks its condition before every pass, including the first — if the condition starts false, the body never runs at all.

int count = 0;
while (count < 5) {
    printf("%d\n", count);
    count++;
}
// 0 1 2 3 4

This is the right shape whenever you don't know the number of iterations ahead of time — reading input until a sentinel value appears, or polling a hardware flag until it's set.

while (!data_ready()) {
    // spin, waiting
}
process(read_data());

do-while

A do-while checks its condition after the body — guaranteeing the body runs at least once, even if the condition is false from the start:

int choice;
do {
    printf("Enter 1-5: ");
    scanf("%d", &choice);
} while (choice < 1 || choice > 5);

This shape fits menus and input-validation loops exactly — you always need to ask at least once before you have anything to validate.

Which one to reach for

LoopCondition checkedMinimum runsBest fit
forBefore each pass0Known iteration count (array traversal, counting)
whileBefore each pass0Unknown iteration count (waiting, reading until done)
do-whileAfter each pass1Body must run at least once (menus, input validation)

break and continue

break
Exits the loop immediately, skipping every remaining iteration — execution resumes at the first statement after the loop.
continue
Skips the rest of the current iteration only and jumps straight to the next one's condition check — the loop itself keeps running.
for (int i = 0; i < 10; i++) {
    if (i == 3) continue;   // skip printing 3, keep looping
    if (i == 7) break;      // stop the loop entirely at 7
    printf("%d\n", i);
}
// 0 1 2 4 5 6

Real-world applications

Gotchas

Field notes