Iterative Statements in C Language: A Beginner-Friendly Guide to Looping and Control Flow
Introduction to Iterative Statements in C
When you want to repeat a task again and again, you use loops. Iterative statements in C help you run a block of code multiple times, saving you from writing the same code again.
In this article, we'll cover looping, entry and exit control, and all related keywords in the simplest possible way — with examples and clear logic.
What is Looping in Programming?
✅ Why Use Loops?
Imagine printing "Hello" 100 times. Would you write printf("Hello"); 100 times? Of course not! That's where loops save time and make the code cleaner.
📘 Types of Loops in C
- while loop – Entry control
- do-while loop – Exit control
- for loop – Entry control
- Nested loops – Loop inside loop
while Loop in C
🧾 Syntax
// code block
}
📌 Example
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Output:
2
3
4
5
Explanation: Loop runs while i is less than or equal to 5.
do-while Loop in C
🧾 Syntax
// code block
} while (condition);
📌 Example
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
Output:
2
3
4
5
Key Point: Even if condition is false at start, the loop runs once.
for Loop in C
🧾 Syntax
// code block
}
📌 Example
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
Output:
2
3
4
5
Why it's useful: Best for when number of iterations is known.
Nested Loops in C
A loop inside another loop is called a nested loop.
📌 Example: Multiplication Table
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d x %d = %d\n", i, j, i * j);
}
}
return 0;
}
switch Case Statement in C
Used when you have many fixed choices like menu items.
🧾 Syntax
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
📌 Example
int main() {
int day = 2;
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Invalid day");
}
return 0;
}
Output: "Tuesday"
break Statement in C
Used to exit a loop or switch early.
📌 Example
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5)
break;
printf("%d\n", i);
}
return 0;
}
Output:
2
3
4
continue Statement in C
Skips current loop iteration and moves to next.
📌 Example
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3)
continue;
printf("%d\n", i);
}
return 0;
}
Output:
2
4
5
goto Statement in C
It jumps directly to a label in code.
📌 Example
int main() {
goto skip;
printf("This will not print");
skip:
printf("Jumped here using goto");
return 0;
}
Output:
Note: Avoid using goto unless necessary. It can make code confusing.
Loop Control Tips and Best Practices
- Always check loop conditions
- Use break to avoid infinite loops
- Keep loops simple and clean
- Avoid too many nested loops
Common Errors in Loops
- Forgetting to update loop variable (causes infinite loop)
- Wrong condition logic
- Misusing break/continue/goto
Real-Life Uses of Loops
- Reading files line by line
- Processing lists or arrays
- Repeating animations or sound effects
- User input handling
Summary of Key Points
- Loops help repeat tasks easily
- while, do-while, for are most used loops
- break exits early, continue skips iteration
- switch handles multi-way decisions
- Use goto carefully
Conclusion
Learning how to repeat actions with loops is one of the most important skills in C programming. Whether you're creating a calculator or running a game loop, knowing how and when to use while, for, do-while, switch, break, continue, and goto will give you full control over your program's flow.
Practice small examples first, and then try building more complex patterns step by step.
FAQs
1. What is the difference between while and do-while?
while checks condition first. do-while runs once and then checks.
2. Which loop is best for known iterations?
for loop is best when you know how many times to repeat.
3. Is using goto a good practice?
Generally no. It can make code hard to understand.
4. Can we use break and continue together?
Yes, but use them wisely to keep code readable.
5. How are switch and if-else different?
Use if-else for ranges or logic; switch for fixed values.
0 Comments
If you have any doubts, Please let me know