The difference between For, While and do while loop with code:
For loop: The for loop is typically used when the number of iterations is known beforehand. It includes initialization, condition checking, and iteration in one line.
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
While loop: The while loop is used when the number of iterations is not known beforehand and is based on a condition. It checks the condition before executing the loop body.
{
int i;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Do while loop: The do-while loop is similar to the while loop but it guarantees that the loop body will be executed at least once, as the condition is checked after the loop body.
{
int i;
do
{
printf("%d\n", i);
i++;
}
while (i <= 5)
return 0;
}
{
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
While loop: The while loop is used when the number of iterations is not known beforehand and is based on a condition. It checks the condition before executing the loop body.
#include <stdio.h>
int main()
{
int i;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Do while loop: The do-while loop is similar to the while loop but it guarantees that the loop body will be executed at least once, as the condition is checked after the loop body.
#include <stdio.h>
int main()
{
int i;
do
{
printf("%d\n", i);
i++;
}
while (i <= 5)
return 0;
}