The assembly line of code β keep stamping until the box is full
The Assembly Line
Imagine an assembly line in a factory. A robot arm picks up a part, stamps it, puts it on the belt, and repeats β hundreds of times. It doesn't get bored, doesn't make mistakes (unless you programmed it wrong), and stops exactly when the box is full.
That's what loops do in C. They repeat a block of code until some condition tells them to stop. C gives you three types of loops, each suited for a different situation:
for β When you know exactly how many times to repeat.
while β When you repeat until some condition changes.
do-while β Like while, but always runs at least once.
The for Loop
The for loop packs everything into one line: initialization, condition, and update. It's the most common loop in C and the one you'll use for counting.
Syntax: for (init; condition; update) { body }
for Loop β Counting
#include <stdio.h>
int main(){
// Count 1 to 5
for(int i =1; i <=5; i++){
printf("%d ", i);
}
printf("\n");
// Count down from 5
for(int i =5; i >=1; i--){
printf("%d ", i);
}
printf("\n");
// Step by 2
for(int i =0; i <=10; i +=2){
printf("%d ", i);
}
printf("\n");
return0;
}
Output
1 2 3 4 5
5 4 3 2 1
0 2 4 6 8 10
The while Loop
The while loop checks its condition before each iteration. If the condition is false from the start, the body never runs at all. Use it when you don't know in advance how many repetitions you need.
while Loop β Unknown Iterations
#include <stdio.h>
int main(){
// How many times can we halve a number before it reaches 0?
int num =1000;
int steps =0;
while(num >0){
num /=2;// Integer division
steps++;
}
printf("It took %d halvings to reach 0\n", steps);
// Sum digits of a number
int n =9472;
int sum =0;
int original = n;
while(n >0){
sum += n %10;// Get last digit
n /=10;// Remove last digit
}
printf("Sum of digits of %d = %d\n", original, sum);
return0;
}
Output
It took 10 halvings to reach 0
Sum of digits of 9472 = 22
The do-while Loop
The do-while loop is the try-then-check loop. It runs the body first, then checks the condition. This guarantees the body executes at least once β perfect for input validation where you need to ask the user for something and then check if it's valid.
do-while β Input Validation
#include <stdio.h>
int main(){
int choice;
// Simulating menu input validation
// (In real code, scanf would read user input each time)
choice =3;// Simulated valid input
do{
printf("Menu: 1=Play 2=Settings 3=Quit\n");
printf("You chose: %d\n", choice);
// In real code: scanf("%d", &choice);
}while(choice <1|| choice >3);
printf("Valid choice: %d\n", choice);
// do-while for generating output at least once
int n =0;
do{
printf("This prints even though n is %d\n", n);
}while(n >0);// Condition is false, but body ran once
return0;
}
Output
Menu: 1=Play 2=Settings 3=Quit
You chose: 3
Valid choice: 3
This prints even though n is 0
break and continue
break smashes the emergency stop button β it immediately exits the loop. continue skips the rest of the current iteration and jumps to the next one. Both work in for, while, and do-while.
break and continue
#include <stdio.h>
int main(){
// break: find the first multiple of 7 over 50
for(int i =51; i <100; i++){
if(i %7==0){
printf("First multiple of 7 over 50: %d\n", i);
break;// Stop searching
}
}
// continue: print odd numbers only
printf("Odd numbers 1-10: ");
for(int i =1; i <=10; i++){
if(i %2==0){
continue;// Skip even numbers
}
printf("%d ", i);
}
printf("\n");
return0;
}
Output
First multiple of 7 over 50: 56
Odd numbers 1-10: 1 3 5 7 9
Nested Loops
A loop inside a loop β the inner loop completes all its iterations for each single iteration of the outer loop. Think of it like clock hands: the second hand goes around 60 times for every tick of the minute hand.
Note: Three loop pitfalls to watch for: 1. Off-by-one errors β Using < when you meant <= (or vice versa). Should it be i < 5 (runs 0-4) or i <= 5 (runs 0-5)? This is the most common loop bug. 2. Infinite loops β Forgetting to update the loop variable in a while loop, or writing a condition that can never be false. Your program hangs and you have to Ctrl+C to kill it. 3. do-while always runs once β If your logic requires skipping the body entirely when the condition is false, use while instead of do-while.
Common Pattern β Summing with a Loop
#include <stdio.h>
int main(){
// Sum of 1 to 100
int sum =0;
for(int i =1; i <=100; i++){
sum += i;
}
printf("Sum of 1 to 100 = %d\n", sum);
// Factorial of 10
long factorial =1;
int n =10;
for(int i =1; i <= n; i++){
factorial *= i;
}
printf("%d! = %ld\n", n, factorial);
// Find max in a set of numbers
int nums[]={34,78,12,95,43,67};
int max = nums[0];
for(int i =1; i <6; i++){
if(nums[i]> max){
max = nums[i];
}
}
printf("Max value: %d\n", max);
return0;
}
Output
Sum of 1 to 100 = 5050
10! = 3628800
Max value: 95
Challenge
Quick check
How many times does the body of for (int i = 0; i < 5; i++) execute?