Mastering Loops in C: A Comprehensive Guide | Episode 3
Introduction to Loops in C
In C programming, loops are essential for repetitive tasks. They allow you to execute a block of code repeatedly under certain conditions.
Types of Loops in C
There are three main types of loops in C:
1. for loop
for (initialization; condition; increment/decrement) {
// Code to be repeated
}
Example:
for (int i = 1; i <= 5; i++) {
printf("Iteration %d\n", i);
}
2. while loop
while (condition) {
// Code to be repeated
}
Example:
int num = 1;
while (num <= 5) {
printf("Number: %d\n", num);
num++;
}
3. do-while loop
do {
// Code to be repeated
} while (condition);
Example:
int x = 1;
do {
printf("Value: %d\n", x);
x++;
} while (x <= 5);
0 Comments