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
Post a Comment