Introduction to Loops
Loops allow programmers to execute a block of code repeatedly as long as a specified condition is true. They make programs efficient and reduce redundancy.
💡 Key Concept
Loops are used for iteration -> executing the same code multiple times until a condition becomes false.
1. The For Loop
Definition
The for loop is used when the number of iterations is known in advance. It consists of three parts: initialization, condition, and increment/decrement.
Syntax
for (initialization; condition; increment/decrement) {
// Code to execute repeatedly
}
Explanation
- Initialization: Executes once at the start
- Condition: Checked before each iteration
- Increment/Decrement: Updates loop variable after each iteration
Example 1: Printing Numbers from 1 to 5
#include <stdio.h>
int main() {
for (int i = 1; i < = 5; i++) {
printf("%d ", i);
}
return 0;
}
Output: 1 2 3 4 5
Example 2: Sum of First 10 Natural Numbers
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i < = 10; i++) {
sum += i;
}
printf("Sum = %d\\n", sum);
return 0;
}
Output: Sum = 55
Flowchart
2. The While Loop
Definition
The while loop is used when the number of iterations is not known in advance. It continues executing as long as the condition remains true.
Syntax
while (condition) {
// Code to execute repeatedly
}
Example 1: Counting from 1 to 5
#include <stdio.h>
int main() {
int i = 1;
while (i < = 5) {
printf("%d ", i);
i++;
}
return 0;
}
Output: 1 2 3 4 5
Example 2: Reverse Counting
#include <stdio.h>
int main() {
int n = 5;
while (n > 0) {
printf("%d ", n);
n--;
}
return 0;
}
Output: 5 4 3 2 1
Flowchart
3. The Do-While Loop
Definition
The do-while loop is similar to the while loop, but it checks the condition after executing the loop body. Hence, the loop executes at least once, even if the condition is false.
Syntax
do {
// Code to execute
} while (condition);
Example 1: Execute at Least Once
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d ", i);
i++;
} while (i < = 5);
return 0;
}
Output: 1 2 3 4 5
Example 2: Demonstrating Condition False Initially
#include <stdio.h>
int main() {
int x = 10;
do {
printf("This executes once!\n");
} while (x < 5);
return 0;
}
Output: This executes once!
Flowchart
Comparison of Loops
Loop Type | Condition Check | Best Use Case | Executes At Least Once? |
---|---|---|---|
for | Before each iteration | When number of iterations is known | No |
while | Before each iteration | When number of iterations is unknown | No |
do-while | After each iteration | When the loop must execute at least once | Yes |
Practice Exercises
- Exercise 1: Write a program to print the multiplication table of a number using a for loop.
- Exercise 2: Find the factorial of a number using a while loop.
- Exercise 3: Display the digits of a number in reverse order using a do-while loop.
Summary
✅ For Loop
Used when the number of iterations is known. Combines initialization, condition, and update in one line.
✅ While Loop
Used when the number of iterations is unknown. Checks condition before executing.
✅ Do-While Loop
Executes the loop body at least once and checks the condition afterward.
💡 Best Practice Tip
Ensure that loop conditions eventually become false to avoid infinite loops. Always update loop variables properly.