for statement or for loop explain with syntax and example
The C for statement has the following form:
for(expression1; expression2; expression3)
statement;
or {block of statements}
expression1 initialises; expression2 is the terminate test; expression3 is the modifier (which may be more than just simple increment);
NOTE: C basically treats for statements as while type loops
For example: int x;
main()
{
for(x=3;x>0;x--)
{
printf("x=%d\n",x);
}
}
...outputs:
x=3
x=2
x=1
...to the screen
All the following are legal for statements in C. The practical application of such statements is not important here, we are just trying to illustrate peculiar features of C for that may be useful:-
for(x=0;((x>3) && (x<9)); x++)
for(x=0,y=4;((x>3) && (y<9)); x++,y+=2)
for(x=0,y=4,z=4000;z; z/=10)
The second example shows that multiple expressions can be separated a ,.
In the third example the loop will continue to iterate until z becomes 0;