#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=0;
char a='a', b='b', c='c';
for(cout<<a<<" ";cout<<b<<" ";cout<<c<<" ")
{
i++;
if(i==10)
break;
}
getch();
}
OUTPUT :-
-----------------------------------------------------------------
a b c b c b c b c b c b c b c b c b c b
-----------------------------------------------------------------
The FOR loop has three parts. The first part is the initialization part, the second one is the condition part and the third part is the updation part i.e incrementing or decrementing.
The syntax of for loop is :-
for(initialization ; condition ; increment / decrement)
{
body of the loop
}
First intialisation is done and then condition is tested and if it is true then the body of the loop is executed. Then the control proceeds to the updation part. When it finishes processing updation then again the condition is tested. If it is true again then the body of the loop is executed one more. This process continues till the condition becomes false.
But the above program is not for illustrating this general FOR loop. Infact it is for showing an interesting facts of this for loop.
Here we haven't used those usual initialization, control, and updation statement but instead we have used "cout" statement (used for outputting results on the screen) in place of those three parts of the FOR loop.
The result I have found is that it behaves similarly. The control passes in the similar sequence as that of the general three statement parts of the FOR loop.
When a c program detects a FOR loop it first proceeds to the initialization part. Similarly in the FOR loop of the above program the statement "cout<<a" gets executed which is in place of the initialization part. Then in a general FOR loop after initialization it checks the condition and if it is true the body of the loop gets executed. In the above program it then executes the middle statement of the FOR loop i.e "cout<<b" which is in place of the condition statement. In this FOR loop there is no condition but instead is a cout statement. Since no decision (true / false) can be taken on this statement the body of the loop will be executed for infinite times unless it is stopped by the break statement inside the body of the loop. In a general FOR loop after executing the body of the loop it proceeds to the updation part and then again it checks for the condition. But in our program we again have given another cout statement in place of the updation part. So after executing the body of the loop it executes the cout statement present in the updation part and then instead of checking for condition it executes the cout statement present in the condition part of the FOR loop. The body of the loop will go on executing since it will never become false (i.e no condition statement is there instead a cout statement is present).
We have used the break statement in our program to limit the execution of the body of the loop for 10 times. Then the control comes out of the loop.