Theory
Sixty students, one program run
The marks program handles one student beautifully. The class has sixty.
Your options today: run the program sixty times (the peon's method), copy-paste the code sixty times (the disaster method), or tell C: "repeat this block until I say stop".
That third option is iteration, the last of your three structured-programming shapes, and it comes in three flavours whose differences are pure exam gold.
Theory
Three kinds of repetition in real life
while is airport security: the guard checks BEFORE you enter, and if you fail the first check, you never enter at all (zero entries possible). do-while is tasting dal while cooking: you taste FIRST, then decide whether to adjust again, the first taste always happens. for is a teacher taking attendance: roll 1 to 60, she knows the count before starting and steps through it mechanically.
Theory
The three loops, formally
- while (condition) { }: test first, run if true, repeat. Fails immediately? The body runs zero times (entry-controlled).
- do { } while (condition);: run first, test after, so the body always runs at least once (exit-controlled). Note the closing semicolon.
- for (init; condition; update) { }: the counted loop.
initruns once,conditionis tested before each round,updateruns after each round:
for (i = 1; i <= 60; i++) reads: start at student 1, continue while within 60, step by one.
At a glance
Choosing your loop
| Loop | Tests | Minimum runs | Built for |
|---|---|---|---|
| while | Before the body | 0 | Unknown count, may skip entirely |
| do-while | After the body | 1 | Menus: show at least once |
| for | Before, with counter | 0 | Known count (60 students) |
Practical
The whole class, one loop
#include <stdio.h>
int main() {
int i, marks, total = 0;
for (i = 1; i <= 5; i++) {
printf("Marks of student %d: ", i);
scanf("%d", &marks);
total = total + marks;
}
printf("Class total: %d\n", total);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
The condition of a while loop is false the very first time. The same condition sits in a do-while. How many times does each body run?
- while: 0 times, do-while: 1 time
- Both run 0 times
- Both run 1 time
- while: 1 time, do-while: 0 times
Show the answer
while: 0 times, do-while: 1 time
while is entry-controlled: a false test at the gate means the body never runs. do-while is exit-controlled: the body has ALREADY run once before the first test. This 0-vs-1 distinction is the most reliably asked loop question in every C paper, often as "differentiate while and do-while".
Think first
Trace the loop
In the code above, the five entered marks are 70, 80, 60, 90, 75. Walk the loop in your head: what is total after each round, and what prints at the end?
Show the answer
total grows 0 → 70 → 150 → 210 → 300 → 375, and the program prints Class total: 375.
Notice the pattern you just used: a running total starting at 0, updated inside the loop. That accumulator pattern reappears in averages, counts and sums for the rest of your programming life.
Watch out
The three loop killers
The stray semicolon: for (i = 1; i <= 5; i++); ends the loop with an empty body; the block below runs once. Same disease as if's semicolon. The infinite loop: forget the update (i++) and the condition never turns false; the program hangs. Off-by-one: i < 5 runs five times starting at 0, i <= 5 runs six starting at 0, count on your fingers in exams, every time.
Theory
All three shapes acquired
Sequence, selection, and now iteration: the marks program uses every structured-programming shape, exactly as promised in Unit 1. Two refinements complete the loop story: loops INSIDE loops (the pattern-printing questions exams adore), and the emergency exits break and continue. Both are next.
Summary
Key takeaways
- while: test before, may run zero times (entry-controlled).
- do-while: test after, always runs at least once (exit-controlled), note the semicolon.
- for (init; condition; update): the counted loop for known repetitions.
- Killers: semicolon after for(...), missing update (infinite loop), off-by-one bounds.
- The running-total accumulator is the loop pattern of a lifetime.
- Memory hook: guard checks first (while), taste first (do-while), attendance count (for).