1. for loop
2. while loop
3. do-while loop
### 1. for loop:
The for loop is typically used when the number of iterations is known.
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("Iteration: %d\n", i);
}
return 0;
}
In this example, the for loop will run 5 times, with i starting at 0 and incrementing until it reaches 5.
### 2. while loop:
The while loop is used when the number of iterations is not known and you want to continue looping while a condition is true.
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("Iteration: %d\n", i);
i++;
}
return 0;
}
The while loop will continue as long as i is less than 5.
### 3. do-while loop:
The do-while loop is similar to the while loop, but it guarantees that the code inside the loop is executed at least once, even if the condition is initially false.
#include <stdio.h>
int main() {
int i = 0;
do {
printf("Iteration: %d\n", i);
i++;
} while (i < 5);
return 0;
}
In this case, the code inside the loop is executed first, and then the condition is checked.
Each loop type has its use case, depending on whether you know the number of iterations beforehand or need to evaluate the condition during runtime.