c - For loop with printf as 3rd argument -


studying computer science final......

i cannot figure example out.....

i understand leaving first argument blank makes act true....

but don't understand leaving variable in second argument accomplishes....

what don't understand how printf statement "updates" variable condition...

  #include<stdio.h>   int main()   {     int x=1, y=1;     for(; y; printf("%d %d\n", x, y))     {       y = x++ <= 5;     }     printf("\n");     return 0;   } 

the output is:

2 1 3 1 4 1 5 1 6 1 7 0 

edit:

i understand for-loop structure part.....

thanks answers - insightful thanks!

a for loop can thought of for (initialization; condition; afterthought)

the first part of loop initialisation. leaving empty fine, indicates have initialised variables required loop.

the y in second expression (or condition) of for loop equivalent y!=0. keeps for loop running until y==0.

the printf in afterthought run @ end of each iteration doesn't change value of y. loop's body change y however.

most textbooks describe this. or see wikipedia or cplusplus.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -