Hi alaios you know the basic syntax of the for loop as
for (initialization;condition;increment)
though this is recommended that all these must be used ;all of parameter of the for loop can be ommited (but you
must have a semicolon )
for simplicity I am giving you the sample code
1]
#include<stdio.h>
int main() {
int i;
for(i=0;i<10;i++) {
printf("the value of i is %d \n",i);
}
return(0);
}
/* The above Code use the standerd format of the for loop.
2]
#include<stdio.h>
int main() {
int i=0;
for(;i<10;i++) {
printf("the value of i is %d \n",i);
}
return(0);
}
/* The above Code has ommited the initilization note the i is initilized outside the for loop.
3]
#include<stdio.h>
int main() {
int i=0;
for(;i<10
{
printf("the value of i is %d \n",i);
i++;
}
return(0);
}
* The above Code has ommited the initilization as wel as increment of i, note the i is initilized outside the for loop and i is incremented in the loop itself.
The above all three programme are same and give same output as
the value of i is 0
the value of i is 1
the value of i is 2
the value of i is 3
the value of i is 4
the value of i is 5
the value of i is 6
the value of i is 7
the value of i is 8
the value of i is 9
Now look at the code which is just variant to the third one
4]
#include<stdio.h>
int main() {
int i=0;
for( ; ; ) {
printf("the value of i is %d \n",i);
i++;
//if (i>9)
//exit(0);
}
return(0);
}
The above code is fully valid and runs note that the syntax of for loop used.
the only difference is this for loop is contineous and to stop the program you have to use ctrl+C.
To overcome this you can test for the value of i inside the loop itself as I done using the if condition just uncommnet the 2 lines and the program provides the same output as the above 3 programmes.
**Note to run the programme just save on your directory with name say test.c
now compile with gcc as
gcc -o test test.c
and finlly on command promt just enter .
./test
So the moral of this story is for ( ;
; is fully functional but
This will not
Regards Junaid