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.

General form of For Loop


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

Program Printing 0 to 10 using For Loop

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int a;
for(a = 0; a <= 10; a++) //loop looping from 0 to 10.
{
cout<<a<<endl; //and printing.
}
getch();
}
Output
0
1
2
3
4
5
6
7
8
9
10

Program For Loop using If Statement

Program

#include<iostream.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 then 4 And less then 8 then
{
cout<<a<<endl; //print 'a' value.
}
}
getch();
}
Output
5
6
7

Program For Loop using Break Statement
Program

#include<iostream.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
{
cout<<"The Statement is Break on 5!";
break; //break Statement.
}
cout<<a<<endl;
}
getch();
}
Output
0
1
2
3
4
The Statement is Break on 5!