Chapter 825 min read
C Loops & Iterative Control Flow
Master repetitive execution using while loops, do-while loops, and break/continue control statements.
Released1972
Core MottoAutomate Repetitive Logic Efficiently
CreatorsDennis Ritchie / Bell Labs
1. Why Loops? Loop Architecture
Loops allow executing a single block of code repeatedly without writing duplicate statements.
Conditions determine whether the loop body continues running or exits.

The Problem with Repetitive Code
Problem Statement:Printing a message 5 times sequentially requires writing 5 separate `printf` statements. Doing this a thousand times is impractical.
Solution:Programming languages provide loop statements to repeat a block of code until a specified stopping condition is reached.
c
#include <stdio.h>
int main() {
// Sequential repetitive print (Without Loops)
printf("Hello, World!\n");
printf("Hello, World!\n");
printf("Hello, World!\n");
printf("Hello, World!\n");
printf("Hello, World!\n");
return 0;
}Basic Loop Logic in C
Loop Control Variable:A counter variable (e.g., `int i = 0`) tracks iteration state.
Condition Evaluation:The loop checks if `i < 5`. If true, the body executes.
Increment Step:Updating `i = i + 1` ensures the loop eventually terminates and avoids an infinite execution cycle.
c
#include <stdio.h>
int main() {
int i = 0;
// Iterative approach using a while loop
while (i < 5) {
printf("Hello, World!\n");
i = i + 1;
}
return 0;
}2. The while Loop
A pre-test loop that evaluates the condition before executing the code block.
If the initial condition is false, the loop body never runs.

while Loop Syntax & Mechanics
Keyword & Condition:Starts with `while` followed by a boolean condition in parentheses `( condition )`.
Body Enclosure:Code statements are enclosed in curly braces `{...}`.
Truth Values:Non-zero values are treated as `true`, while `0` is treated as `false`.
Exit Mechanism:As soon as the condition evaluates to false, execution resumes at the statement following the loop body.
c
#include <stdio.h>
int main() {
int count = 0;
while (count < 5) {
printf("Hello, World!\n");
count++;
}
return 0;
}3. The do...while Loop
A post-test loop that evaluates its condition after executing the body.
Guarantees that the loop statements execute at least once.

do...while Syntax & Mechanics
Guarantee:Executes the loop body first, then checks the condition at the bottom.
Mandatory Semicolon:Note the semicolon after the condition: `while ( condition );`.
Use Cases:Ideal for menu systems, input validation, or tasks that must execute at least once regardless of initial state.
c
#include <stdio.h>
int main() {
int i = 0;
do {
printf("Hello, World!\n");
i = i + 1;
} while (i < 5);
return 0;
}4. Control Statements: break & continue
The break statement exits a loop immediately.
The continue statement skips the current iteration and jumps to the next evaluation.
The break Statement
Immediate Termination:Forces an immediate exit from the innermost active loop.
Execution Resumption:Program flow jumps directly to the statement immediately following the loop body.
c
#include <stdio.h>
int main() {
int i = 0;
do {
printf("Hello, World!\n");
i = i + 1;
// Exits early after 3 iterations
if (i == 3) {
break;
}
} while (i < 5);
return 0;
}The continue Statement
Skip Iteration:Bypasses the rest of the code in the current iteration without exiting the loop entirely.
Next Evaluation:Re-evaluates the condition for `while` / `do-while` loops or advances the update step in `for` loops.
c
#include <stdio.h>
int main() {
int i = 0;
do {
// Skips printing when i equals 3
if (i == 3) {
i = i + 1;
continue;
}
printf("Hello, World!\n");
i = i + 1;
} while (i < 5);
return 0;
}Classroom REMINDER
How does a while loop differ from a do...while loop when the initial condition is false?
Takeaway Goal: A while loop tests its condition before executing and will run zero times if the condition is false initially. A do...while loop checks its condition at the end, guaranteeing that the body executes at least once.