1. Introduction
Loops let a C program repeat a block of statements without duplicating code, driven by a condition that determines when repetition should stop. C provides three looping constructs — for, while, and do-while — which differ mainly in when the controlling condition is checked and how the initialization/update steps are organized.
Cricket analogy: A loop is like a bowler repeating the same run-up and delivery action over after over without rewriting instructions each time; for, while, and do-while are like three different net-practice routines that differ in when the coach checks if you should bowl another ball.
Choosing the right loop improves readability: for is preferred when the number of iterations is known or counter-driven, while is preferred for condition-driven repetition where the iteration count isn't known in advance, and do-while is used when the loop body must run at least once regardless of the condition.
Cricket analogy: Use a for loop like bowling a fixed six-ball over (known count); use a while loop like batting until you get out (condition-driven, unknown balls); use a do-while loop like a toss that must happen once before deciding who bats first.
2. Syntax
2.1 for loop
for (initialization; condition; update) {
statement(s);
}2.2 while loop
while (condition) {
statement(s);
}2.3 do-while loop
do {
statement(s);
} while (condition); // note the trailing semicolon3. Explanation
3.1 for loop
The for loop bundles initialization, condition check, and update into one header, executed in this order: initialization runs once; the condition is checked; if true, the body runs, then the update executes, and the condition is checked again — repeating until the condition becomes false. Because the condition is checked BEFORE the first iteration, a for loop's body can execute zero times if the condition is initially false. Any of the three header components can be omitted (e.g. for (;;) creates an infinite loop), and the comma operator can initialize or update multiple variables.
Cricket analogy: The for loop is like a set over where you first set the field (init), check if there are balls left (condition), bowl the ball (body), then update the over count; if the umpire calls no balls left before bowling even starts, zero deliveries happen, and an unbounded practice session, like the empty for(;;), bowls forever.
3.2 while loop
The while loop checks its condition BEFORE each iteration, including the first. If the condition is false at the very start, the body never executes. while is an entry-controlled (pre-tested) loop, best suited when the number of repetitions depends on a condition evaluated at runtime rather than a fixed counter, such as reading input until a sentinel value is seen.
Cricket analogy: A while loop is like a batsman checking the scoreboard before every ball to see if the target is reached; since the check happens before facing the first ball, an already-met target means zero balls are needed — ideal for chasing a total where the number of balls isn't fixed in advance.
3.3 do-while loop
The do-while loop checks its condition AFTER executing the body, making it an exit-controlled (post-tested) loop. Consequently, the body of a do-while always executes at least once, even if the condition is false from the start. This makes do-while ideal for menu-driven programs where an action (like displaying a menu) must happen at least once before checking whether to repeat. Note the mandatory trailing semicolon after while (condition) in a do-while — forgetting it is a compile-time syntax error.
Cricket analogy: A do-while loop is like a coin toss that always happens once before the umpire checks whether extra overs are needed — the toss (body) runs at least once regardless of the game situation, just as a menu-driven scorer app always shows the menu once before checking if the user wants to continue; forgetting the closing semicolon is like forgetting to record the toss result.
break immediately terminates the nearest enclosing loop (or switch), transferring control to the statement right after it. continue skips the remaining statements in the current iteration and jumps to the next iteration's condition check (for while/do-while) or to the update step (for for). Both work identically across all three loop types.
A common exam trap is confusing when the condition is tested. For identical-looking bodies, while and for can execute the body zero times, but do-while always executes it at least once. Also watch for infinite loops caused by forgetting to update the loop control variable inside a while/do-while body — the for loop's structure makes this mistake slightly less likely because the update is visible in the header.
4. Example
#include <stdio.h>
int main(void) {
// for loop: print 1 to 5
printf("for: ");
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
// while loop: sum digits of a number
int n = 1234, sum = 0;
printf("while digit sum: ");
while (n != 0) {
sum += n % 10;
n /= 10;
}
printf("%d\n", sum);
// do-while loop: runs at least once even though condition is false
int count = 10;
printf("do-while: ");
do {
printf("%d ", count);
count++;
} while (count < 5); // false immediately, but body already ran once
printf("\n");
// break and continue demo
printf("break/continue: ");
for (int i = 1; i <= 10; i++) {
if (i == 7) break; // stop the loop entirely at 7
if (i % 2 == 0) continue; // skip even numbers
printf("%d ", i);
}
printf("\n");
return 0;
}5. Output
for: 1 2 3 4 5
while digit sum: 10
do-while: 10
break/continue: 1 3 5 6. Key Takeaways
- for combines initialization, condition, and update in one header; ideal for counter-driven, known-iteration-count loops.
- while checks its condition before every iteration (entry-controlled) and may run zero times.
- do-while checks its condition after the body runs (exit-controlled) and always runs at least once.
- do-while requires a trailing semicolon after the while(condition) clause — a frequent syntax-error trap.
- break exits the nearest enclosing loop entirely; continue skips to the next iteration.
- Forgetting to update the loop-control variable inside while/do-while bodies is a common cause of infinite loops.
Practice what you learned
1. Which loop is guaranteed to execute its body at least once, regardless of the condition?
2. What is printed by: int i = 5; while (i < 5) { printf("%d", i); i++; }
3. In `for (int i = 0; i < 5; i++) { if (i == 2) continue; printf("%d", i); }`, what is printed?
4. What must follow the condition in a do-while loop's header?
5. Which statement about `for (;;) { ... }` is correct?
Was this page helpful?
You May Also Like
if-else in C
Learn C's if, if-else, and else-if ladder with syntax, dangling-else pitfalls, nested examples, and exam-style practice questions.
switch Statement in C
Understand the C switch statement — syntax, fall-through, break, and default rules — with a correct example and common exam traps.
Arrays in C
Learn C arrays: 1D and 2D declarations, initialization, passing to functions, pointer decay, and out-of-bounds pitfalls with examples.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics