Imagine you’re playing your favorite game 🎮 and need to collect 100 coins.
Would you like to press a button 100 times manually… or just set a loop that does it for you?
That’s exactly what loops in C are for — they let you repeat tasks efficiently without rewriting the same code again and again.
That’s exactly what loops in C are for — they let you repeat tasks efficiently without rewriting the same code again and again.
🌟 What is a Loop?
A loop is a programming construct that allows a set of instructions to be executed multiple times
until a condition is met.
In C, there are three main looping constructs:
- for loop
- while loop
- do...while loop
The for Loop (The All-in-One Loop)
The for loop is like your favorite playlist on repeat — it knows when to start, how long to keep
going, and how to move to the next song.
🔹 Syntax:
for(initialization; condition; update) {// statements to be repeated}
Initialization → Start value (e.g., i = 1).
Condition → Loop runs as long as this is true (e.g., i <= 10).
Update → Value change after each iteration (e.g., i++).
🔹 Example Program: Print Numbers 1 to 10
#include <stdio.h>int main() {int i;for(i = 1; i <= 10; i++) {printf("%d\n", i);}return 0;}▶️ Output
1
2
3
4
5
6
7
8
9
10
🔹 Flowchart of for Loop
🔹 Example 2: Sum of First 10 Natural Numbers
#include <stdio.h>int main() {int i, sum = 0;for(i = 1; i <= 10; i++) {sum += i;}printf("Sum = %d\n", sum);return 0;}▶️ Output
Sum = 55
Variations of for Loop
🔹 1. Skipping Initialization or Update
All three parts (initialization; condition; update) are optional!
Example: initialization done outside, only condition + update inside:
int i = 1;for(; i <= 5; i++) {printf("%d\n", i);}If you don’t give a condition, it defaults to true forever 😲for(;;) {printf("This will run forever! Press Ctrl+C to stop.\n");}
👉 Real-world usage: server programs, game loops, continuously listening to input.
🔹 3. Decrementing for Loop
Count down instead of up:
for(int i = 10; i >= 1; i--) {printf("%d\n", i);Output:
prints 10 down to 1.
🔹 4. Using Multiple Variables
You can control more than one variable in the same loop:
for(int i = 1, j = 10; i <= j; i++, j--) {printf("i = %d, j = %d\n", i, j);}
Output pairs like:
i = 1, j = 10
i = 2, j = 9
...
A for loop inside another → useful for patterns, tables, matrices.
Example: Multiplication table (1–5):
#include <stdio.h>int main() {for(int i = 1; i <= 5; i++) {for(int j = 1; j <= 10; j++) {printf("%d x %d = %d\n", i, j, i*j);}printf("\n"); // blank line after each table}return 0;}
🔹 6. The Creative for Loop
Since the three parts are flexible, you can even do funky things:
for(printf("Start\n"); ; printf("Repeat\n")) {// Infinite loop that prints "Repeat" each time}
Not practical for real coding, but shows how flexible for can be.
The while Loop (The Guard Loop)
The while loop checks the condition before running the block. If the condition is false at the start,
it won’t run at all.
Syntax :
while(condition) {// statements}
Example: Print numbers 1 to 5
#include <stdio.h>int main() {int i = 1;while(i <= 5) {printf("%d\n", i);i++;}return 0;}
▶️ Output
1
2
3
4
5
Let's show some codes of for and while loop that does not execute even once.
for Loop That Doesn’t Execute
Example:
#include <stdio.h>int main() {int i;// Start i = 10, but condition says i <= 5 → already falsefor(i = 10; i <= 5; i++) {printf("i = %d\n", i);}return 0;}
**
Explanation
- Initialization: i = 10.
- Condition check: i <= 5 → false.
- Since the condition is false at the start, loop body is skipped completely.
Output
(no output)
while Loop That Doesn’t Execute
Example
#include <stdio.h>int main() {int x = 20;// Condition is false right awaywhile(x < 10) {printf("x = %d\n", x);x++;}return 0;}
Explanation
- Initial value: x = 20.
- Condition check: x < 10 → false.
- The body is skipped, program exits the loop immediately.
Output
(no output)
🧠 Key Takeaway
Both for and while loops check their condition before entering the loop body.
👉 If the condition is false at the very beginning, the loop will not execute even once.
The do...while Loop (The Optimist Loop)
The do...while loop executes the block at least once, even if the condition is false at the
beginning
Syntax:
do {// statements} while(condition);Example: Print numbers 1 to 5
#include <stdio.h>int main() {int i = 1;do {printf("%d\n", i);i++;} while(i <= 5);return 0;}▶️ Output
1
2
3
4
5
---
---
✅ Why do...while Executes At Least Once
Unlike for and while, which check the condition before running, the do...while loop checks the
condition after the loop body.
That means:
- Loop body executes once.
- Condition checked.
- If true → repeat. If false → exit.
So even if the condition is false initially, the loop body still runs once.
Example: Prove It in Code:
#include <stdio.h>int main() {int x = 20;do {printf("This message will print at least once! x = %d\n", x);x++;} while(x < 10);return 0;}
Step-by-Step Walkthrough
- x = 20 at the start.
- The loop body executes → prints the message once.
- Then the condition x < 10 is checked. Since 20 < 10 is false, loop exits immediately.
Output
This message will print at least once! x = 20
🧠 Key Takeaway
while and for loops: Condition checked first → may not run at all.
do...while loop: Condition checked later → runs minimum once, even if the condition is false.
Can for, while, and do...while Loops Be Used Interchangeably?
Short answer: Yes… but with a twist.
All three loops can be used to solve most repetition problems, but the way they’re structured
makes them better suited for different scenarios.
for Loop vs while Loop
Both for and while are entry-controlled loops (condition checked first).
So, in theory, anything you can do with a for, you can also do with a while.
Example: Print numbers 1–5
👉 Using for loop and while loop:
for(int i = 1; i <= 5; i++) {printf("%d\n", i);}
int i = 1;while(i <= 5) {printf("%d\n", i);i++;}✅ Both give the same output:
1
2
3
4
5
So yes, for and while can often be swapped.
do...while Loop Here’s where things get interesting.
Here’s where things get interesting.
Unlike for and while, a do...while loop always runs at least once. That makes it not
fully interchangeable in situations where the loop may need to be skipped entirely.
Example: Condition False at Start
int x = 20;while(x < 10) { // This never runsprintf("x = %d\n", x);}int x = 20;do { // This runs once before checkingprintf("x = %d\n", x);} while(x < 10);👉 Output difference:
- while → no output.
- do...while → prints x = 20 once.
🧠 Practical Guideline
- Use for → when you know exactly how many times to repeat (like printing first 10 numbers, calculating sum of N numbers).
- Use while → when you don’t know how many times beforehand, and the loop depends on a condition (like reading input until user presses 0).
- Use do...while → when you want the body to execute at least once (like menu-driven programs, asking the user “Do you want to continue?”).
🎯 Key Takeaways
- Technically: for, while, and do...while are interchangeable in many cases.
- Practically: choose the loop that makes your code clearer and more natural for the problem.
👉 Pro coders don’t just write working code — they write readable code. So picking the right
loop matters!
✨ Wrap-Up
- Loops save you from writing repetitive code.
- for loop → best when you know how many times you want to run.
- while loop → best when you don’t know the number of iterations upfront.
- do...while loop → best when you need at least one execution.
So next time you need to print a multiplication table, calculate sums, or repeat a process,
remember: loops have your back!
Post a Comment