Quiz Entry - updated: 2026.07.10
What are the three loop constructs in C?
while tests before the body (0+ runs), do-while tests after (1+ runs), and for bundles init/test/update for counted loops.
* while tests before the body (0+ runs), do-while tests after (1+ runs), and for bundles init/test/update for counted loops. *
while - check THEN execute (0 or more iterations):
while (condition) {
statement;
}
// Example:
while (a && *a) {
c += *a;
}
do-while - execute THEN check (1 or more iterations):
do {
statement;
// Note the semicolon!
} while (condition);
// Example: Always runs at least once
do {
c += *a;
} while (a && *a);
for - init; test; update:
for (init; condition; update) {
statement;
}
// Example:
for (i = 0; i < N; i++) {
c += a;
}
// Multiple variables:
for (i = 0, c = 7; i < N; i++, c--) {
c += a;
}
Tip: Use for when you know the iteration count, while when you don't, do-while when you need at least one iteration.