For Loop in C

For Loop in C programming is a control flow statement that allows code to be repeatedly executed and repeat a specific block of code for a fixed number of times.

For Loop


for(initialise counter ; test counter ; increment counter)
{
do this;
}

Program Printing 0 to 10 using For Loop

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
for(a = 1; a <= 10; a++) //loop looping from 1 to 10.
{
printf("%d\n", a); //and printing.
}
getch();
}
Output
1
2
3
4
5
6
7
8
9
10

Program For Loop using If Statement

Program

#include<stdio.h>
#include<conio.h>
void main()
{
for(int a = 0; a <= 10; a++) //loop looping from 0 to 10.
{
if(a > 4 && a < 8) //if 'a' value greater than 4 And less than 8 then
{
printf("%d", a); //print 'a' value.
}
}
getch();
}
Output
567

Program For Loop using Break Statement
Program

#include<stdio.h>
#include<conio.h>
void main()
{
for(int a = 0; a <= 10; a++) //loop looping from 0 to 10.
{
if(a == 5) //if 'a' value Equal to 5 then
{
printf("The Statement is Break on 5!");
break; //break Statement.
}
printf("%d\n", a);
}
getch();
}
Output
0
1
2
3
4
The Statement is Break on 5!