For Loop • While Loop • Do‑While Loop
Loops let a program repeat a block of code multiple times. They are useful when you need to run the same logic again and again — like counting, processing lists, or repeating user prompts.
A loop basically says: "Do this task again until a condition is met."
There are three types of loops in C:
A for loop lets you repeat something many times automatically. It is used when you already know how many times you want the repetition to happen.
A for loop has three parts:
1. Initialization – where the loop starts
2. Condition – when the loop should stop
3. Update – what happens after each repeat (usually increasing or decreasing a number)
for (initialization; condition; update) {
// code that repeats
}
A for loop repeats code step-by-step. To understand it, imagine the loop follows a fixed routine every time it runs:
1. Run the initialization (this happens only once)
2. Check the condition
- If the condition is true → run the loop body
- If the condition is false → stop the loop
3. After the loop body finishes → update the variable
4. Go back to step 2
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
Step 1: Initialization
The loop starts by setting i = 0. This happens only once.
Step 2: Condition Check
The loop asks: “Is i < 5?“
If yes → continue
If no → stop
Step 3: Run the loop body
Since i = 0 is less than 5, it prints 0.
Step 4: Update
After printing, the loop does i++ → now i becomes 1.
Now it repeats the cycle:
1 < 5? yes → print 1 → update to 22 < 5? yes → print 2 → update to 33 < 5? yes → print 3 → update to 44 < 5? yes → print 4 → update to 55 < 5? no → loop stopsThe loop ends because 5 is NOT less than 5. So the loop exits and moves on to the rest of the program.
i = 0i by 1 every timeSometimes you don’t want to increase the number by 1. You can choose how much the number changes every time the loop repeats.
for (int i = 0; i <= 10; i += 2) {
printf("%d ", i);
}
Step 1: Initialization
The loop starts by setting i = 0.
Step 2: Condition Check
It asks: “Is i <= 10?”
If yes → run the loop
If no → stop
Step 3: Run the loop body
Since 0 <= 10 is true, it prints 0.
Step 4: Update
Instead of i++, this loop uses i += 2.
This means: i = i + 2.
So now i becomes 2.
Now the loop repeats:
2 <= 10? yes → print 2 → update to 44 <= 10? yes → print 4 → update to 66 <= 10? yes → print 6 → update to 88 <= 10? yes → print 8 → update to 1010 <= 10? yes → print 10 → update to 1212 <= 10? no → loop stops
The loop ends when i becomes 12, because 12 is NOT less than or equal to 10.
A decrementing loop is a loop that goes backward. Instead of increasing the number each time, it decreases it.
for (int i = 5; i >= 1; i--) {
printf("%d\n", i);
}
Step 1: Initialization
The loop starts by setting i = 5.
Step 2: Condition Check
It asks: “Is i >= 1?”
If yes → run the loop
If no → stop
Step 3: Run the loop body
Since 5 >= 1 is true, it prints 5.
Step 4: Update
The loop uses i--, which means: i = i - 1.
So now i becomes 4.
Now the loop repeats:
4 >= 1? yes → print 4 → update to 33 >= 1? yes → print 3 → update to 22 >= 1? yes → print 2 → update to 11 >= 1? yes → print 1 → update to 00 >= 1? no → loop stopsThe loop stops because 0 is NOT greater than or equal to 1.
break is used when you want the loop to stop immediately, even if the condition is still true.
for (int i = 0; i < 10; i++) {
if (i == 5) break;
printf("%d\n", i);
}
Step 1: Initialization
The loop starts with i = 0.
Step 2: Condition Check
It asks: “Is i < 10?”
If yes → run the loop
If no → stop
Step 3: Loop Body Starts
Since i = 0, the loop checks the line:
if (i == 5) break;
This asks: “Is i equal to 5?”
Since it's not, the loop prints 0.
Step 4: Update
i++ makes i become 1.
Now the loop repeats:
What happens at i = 5?
if (i == 5) break;
Now the condition is true, so break runs. This instantly stops the entire loop.
No printing happens for 5, 6, 7, 8, or 9.
i becomes 5, break stops the loopcontinue does NOT stop the loop. It simply skips the rest of the current loop step and jumps to the next one.
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) continue;
printf("%d\n", i);
}
Step 1: Initialization
The loop starts with i = 0.
Step 2: Condition Check
It asks: “Is i < 10?”
If yes → continue
If no → stop
Step 3: Loop Body Starts
The loop checks:
if (i % 2 == 0) continue;
i % 2 means “remainder when dividing by 2”.
If the remainder is 0 → the number is even.
Important:
Whenever continue runs, the loop instantly jumps to the update step and then checks the condition again.
It does NOT print anything for that value of i.
while Loop
A while loop repeats code as long as the condition is true.
Use it when you don’t know how many times something needs to repeat.
Real-life example: You keep checking if your download is done. While it’s not done → keep waiting.
while (condition) {
// repeated code
}
This loop starts at 0 and keeps printing numbers until it reaches 4.
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++; // update
}
i = 0i < 5? yes → print 0 → i becomes 11 < 5? yes → print 1 → i = 2i = 5, condition is false → loop stopsThis loop goes backward from 5 to 1.
int i = 5;
while (i >= 1) {
printf("%d\n", i);
i--; // decrease
}
Increase the number by 2 each time.
int i = 0;
while (i <= 10) {
printf("%d ", i);
i += 2;
}
This loop never ends because the condition is always true.
while (1) { // 1 means true
printf("Running...\n");
}
break: stop the loop immediately.
int i = 0;
while (i < 10) {
if (i == 5) break;
printf("%d\n", i);
i++;
}
continue: skip this loop step and continue to the next one.
int i = 0;
while (i < 10) {
i++; // update goes first
if (i % 2 == 0) continue;
printf("%d\n", i);
}
Imagine checking your phone battery. While battery > 20% → keep using phone normally. When battery <= 20% → stop and charge.
int battery = 100;
while (battery > 20) {
printf("Battery OK: %d%%\n", battery);
battery -= 5; // using phone reduces battery
}
do...while Loop
A do...while loop runs the code at least once before checking the condition.
After running, it checks the condition — if true, it repeats.
Real-life example: You taste food while cooking. You always taste once, then decide if you need to taste again.
do {
// repeated code
} while (condition);
This prints numbers from 0 to 4.
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);
i < 5? yes → repeati becomes 5Runs at least once, even if starting value is small.
int i = 5;
do {
printf("%d\n", i);
i--;
} while (i >= 1);
Even though the condition is false, the body runs one time.
int x = 10;
do {
printf("Runs once!\n");
} while (x < 5); // false
Increase by 3 each time.
int i = 0;
do {
printf("%d ", i);
i += 3;
} while (i <= 12);
break: stop the loop immediately.
int i = 0;
do {
if (i == 4) break;
printf("%d\n", i);
i++;
} while (i < 10);
continue: skip this step and continue to next.
int i = 0;
do {
i++;
if (i % 2 == 0) continue;
printf("%d\n", i);
} while (i < 10);
Asking for a password: You always ask once, and if wrong, you keep asking.
int attempts = 0;
int correct = 0;
do {
printf("Enter password:\n");
attempts++;
// fake check for example
if (attempts == 3) correct = 1;
} while (!correct);
while → checks the condition **first** (may run 0 times)do...while → runs **once before checking** (guaranteed 1 execution)
int i = 5;
while (i < 5) {
printf("Hello\n");
i++;
}
Output:
(No output)
Reason: Condition i < 5 is false from the start → loop never runs.
int i = 5;
do {
printf("Hello\n");
i++;
} while (i < 5);
Output:
Hello
Reason: Body runs once before checking the condition.
Choose while when:
Real-life examples:
Choose do...while when:
Real-life examples:
// Loading...