Reputation: 139
Studying for a computer science final......
I really cannot figure this example out.....
I understand that leaving the first argument blank makes it act like TRUE....
but I don't understand what leaving a variable in the second argument accomplishes....
What I don't understand the most is how a printf statement "updates" the 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 now the for-loop structure part.....
Thanks for the answers - very insightful thanks!
Upvotes: 6
Views: 6754
Reputation: 786
Although Answer provided by simonc is perfect, there is also a practical solution. Type this program in Visual Studio, add a breakpoint at start of for loop and run each statement using F10 key. It'll clearly show you what flow is taken at execution time. It'll help clear all your doubts. Also don't forget to keep a watch on variables' values which will help further. This was what helped me through most of my doubts. Enjoy coding!
Upvotes: 0
Reputation: 1018
A trick I found early on was, if I couldn't understand the for loop, try and break it down into an equivalent while loop. You can also paren things to make them more readable (as long as you follow the order of operations on your parens to not screw up the evaluation. Your loop would look like this with those changes:
#include<stdio.h>
int main()
{
int x=1, y=1;
while(y)
{
y = (x++ <= 5);
printf("%d %d\n", x, y)
}
printf("\n");
return 0;
}
With those couple changes it makes it easy to see that your print statement isn't changing/updating anything, but is only printing the resultant of your y value.
Upvotes: 2
Reputation: 557
Leaving a variable y implies the loop will run till y is true
loop will stop when y becomes false
Now this condition that you have written in body evaluates value of y
y = x++ <= 5;
Whenever x++ <= 5 implies y is true that is 1
So it starts with x=1 and prints until value of x becomes 7
and y=x++ <= 5; returns false and loop exits.
Upvotes: 0
Reputation: 42165
A for
loop can be thought of as for (INITIALIZATION; CONDITION; AFTERTHOUGHT)
The first part of the loop is for initialisation. Leaving this empty is fine, it just indicates that you have already initialised any variables required by the loop.
The y
in the second expression (or condition) of the for
loop is equivalent to y!=0
. It keeps the for
loop running until y==0
.
The printf
in the afterthought is run at the end of each iteration but doesn't change the value of y
. The loop's body does change y
however.
Most textbooks will describe this. Or see Wikipedia or cplusplus.
Upvotes: 16
Reputation: 9071
Consider this structure in a for loop:
for (a; b; c)
{
d;
}
This is what will happen:
What's happening in yours at the end is that the "c" part of the code is printing the value of y
and it happens to be 0
. The condition is then evaluated. Since y == 0
, the for loop will break because 0
is equivalent to false
.
Upvotes: 6
Reputation: 173
This runs the program until y is 0 because 0 as an integer is also False
Upvotes: 0