The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Control Loops In C

What Is Loop

A loop enables a programmer to execute a statement or block of statement repeatedly. The need to repeat a block of code arise in almost every program.

C Control Loops

Varieties Of Loop

There are three kinds of loop statements you can use. They are

  • for loop
  • while loop
  • do while loop

For Loop Program

In for loop control statement, initialization, condition and increment are all given in the same loop.

for.c
#include <stdio.h>
int main()
{
int i;
for(i=0; i<10; i++)
{
printf("%d ",i);
}
return 0;
}
  • 0 1 2 3 4 5 6 7 8 9

Note:

Here, for loop will iterate until the conditional statement at the center of for-loop fails.

While Loop Program

In While loop, initialization, condition and increment are all done in different lines.

while.c
#include <stdio.h>
int main()
{
int i = 0;
while(i<10)
{
printf("%d ",i);
i++;
}
return 0;
}
  • 0 1 2 3 4 5 6 7 8 9

Note:

Here, while condition iterate until the condition inside it become false.

Do While Program

Here, it is same as while loop, but it will execute a block of code once irrespective of condition.

dowhile.c
#include <stdio.h>
int main()
{
int i = 0;
do
{
printf("%d ",i);
i++;
}while(i>10);
return 0;
}
  • 0

Note:

Though 0 > 10 tends to false block of code following do was executed successfully.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report