Loop in C programming
I can explain C programming loops.
Loops are control structures that allow you to repeat the execution of a block of code as long as a given condition is met. They are used to automate monotonous processes and avoid writing the same code several times. C programming provides three types of loops: for loops, while loops, and do-while loops.
for
Loop: The for loop is used when you know how many times you want the loop to run. It has the following syntax:
for (initialization; condition; increment/decrement) {
// Code that will be run multiple times
}
Example:
#include <stdio.h>
#include<conio.h>
void main() {
for (int i = 1; i <= 5; i++) {
printf("Iteration %d\n", i);
}
}
while
Loop: The while loop is used to execute a block of code repeatedly as long as a given condition remains true. It has the following syntax: c
while (condition) {
//Code that will be run multiple times
}
Example:
#include <stdio.h>
#include<conio.h>
void main() {
int i = 1;
while (i <= 5) {
printf("Iteration %d\n", i);
i++;
}
}
do-while
Loop: Similar to the while loop, the do-while loop always executes at least once because the condition is assessed after the body of the loop. It has the following syntax
do {
//Code that will be run multiple times
} while (condition);
Example:
#include <stdio.h>
#include<conio.h>
void main() {
int i = 1;
do {
printf("Iteration %d\n", i);
i++;
} while (i<= 5);
}