The odd +=2; command will only call at the end of each loop, and before the i<=n; is evaluated.
So If every line is a run through the loop at the line square += odd;
square = 4 (odd = 3 , i = 1)
square = 9 (odd = 5 , i = 2)
square = 16 (odd = 7 , i = 2)
etc.
EDIT: This is probably the most confusing way of writing this snippit. Either just use one indexing variable "i" for the for loop or use a while loop.
Code:
int i = 1;
int square = 1;
int odd = 3;
while ( i <= n ) {
printf ("%10d%10d\n", i, square) ;
square += odd;
i++;
odd += 2;
}
Well, no need to re-write the while loop when it's right here. But I still don't understand what I'm doing wrong in the for loop yet. I can write my expectations below for the while loop, and what I expect the code to do is what it does, so that's good.
In the above while loop example, i and square are already initialized at 1 respectively, so the first printf line is a given.
i is 1 and square is 1,
then, (in this order according to the code)
square adds odd to itself, odd is currently 3 (because it hasn't been modified yet), so square becomes 4.
then, i increments and becomes 2 (so here we already have our next printf line values ready to go as i is now 2 and square is now 4)
then, lastly, odd adds 2 to itself and becomes 5. So we return to the top of the program, before the while statement and have the values,
i =2
square = 4
odd = 5
so the next printf is now i is 2 and square is 4
then, (in this order according to the code again)
square adds odd to itself, odd is currently 5 (because it hasn't been modified yet), so square becomes 9
then, i increments and becomes 3 (so here we already have our next printf line values ready to go as i is now 3 and square is now 9)
then, lastly, odd adds 2 to itself and becomes 7. So we return to the top of the program, before the while statement and have the values,
i =3
square =9
odd = 7
so the next printf is now i is 3 and square is 9
then repeat the steps above until we have finished the output based on the number of squares the user entered.
I will run back over to the for loop, and see if I can figure out a logical explanation for why it works also.
Thanks!
EDIT: Okay, I've compared the way I'm mentally evaluating the while loop vs. the for loop. The while loop I am doing right, and I can see why it works, it seems to me that the while loop is a bit more straightforward and logical in my opinion. (but my book wants me to do this with the for loop, so I must learn it) Basically, I'm printing the i and square values right off the bat, then AFTER that I'm adding odd to square, incrementing i, and adding 2 to odd, then returning to the top of the program with new values for each. Easy enough, no problem. It works everytime through on paper.
But with the for loop
Code:
for (i = 1, square = 1, odd = 3 ; i <= n; odd += 2, ++i) {
printf ("%10d%10d\n", i, square) ;
square += odd ;
I'm printing i and square right off the bat too, like the while loop. So if I am pretending to be the compiler I'm looking at it like this (this is where I want someone to correct me and tell me why I'm wrong) First I (the compiler)look at the i value, then I look at the square value, (they are both 1 and I print the i and the square for each) then the 3rd step is I (the compiler) look at odd = 3, I don't print that value so odd remains 3 where it is. then I (the compiler) check to see if i <= n, (it is at this point so I continue), then I run into the odd +=2 statement. (I the compiler already have odd = 3 in my memory as I just read that line a second ago, so I add 2 to 3 and now make odd =5 (which I know is wrong, but why? why is this step wrong? I'm going in order from left to write reading the loops statements) then next I (the compiler) come across and read the ++i, so I increment i and make i = 2 now.
I guess technically this is where the print actually happens, so it prints 1 for i and 1 for square..
Then the next step I (the compiler) reads is that square adds odd to itself. Square was 1 (as I just printed it), (I (the compiler) already added 2 to odd as I was reading expr 3 in the for loop above, so I already have odd set to 5.) So or course when I add odd to square, or (5 to 1) I get square is now 6, which is wrong, it should be 4.
Code:
for (i = 1, square = 1, odd = 3 ; i <= n; odd += 2, ++i) {
printf ("%10d%10d\n", i, square) ;
square += odd ;
//////////I think it just hit me//////////////
/////I've just stopped below, I think it hit me. After the printf completes, we don't go back to expr 3 above [odd +=2, ++i ] as I have been inclined to do, then move to the [square += odd] Which gives the wrong output.
/////We (the compiler I mean) goes from the printf line of the loop to the [square += odd] segment, then the compiler moves back to the expr 3 in the for loop statement above. Because if the compiler goes to [square += odd] first, then odd is still 3 at this point, and square becomes 4. Then the compiler returns to expr 3 and adds 2 to odd, and increments i. Then if this path is followed over and over, it will be correct.
I think this is what the problem was for me. I was thinking the compiler was reading like this.
first: (i = 1, square = 1, odd = 3) then second: (i <= n) then third: compiler was printf'ing, then fourth: compiler reads (odd += 2, ++i) then fifth: compiler reads (square += odd), then sixth, all values have been modified by the compiler and we return to expr 1 to do it all over again, (which was always giving me the wrong result)
I think the correct order for the compiler to do it's work is.
first: (i = 1, square = 1, odd = 3) then second: (i <= n) then third: compiler was printf'ing, then fourth: compiler reads (square += odd) then fifth: compiler reads (odd += 2, ++i), then sixth, all values have been modified by the compiler and we return to expr 1 to do it all over again, (which now always gives the correct output)
Have I got it?
Instead of this:
First: (i = 1, square = 1, odd = 3)
Second: (i <= n)
Third: compiler was printf'ing
Fourth: compiler reads (odd += 2, ++i)
Fifth: compiler reads (square += odd)
Sixth: All values have been modified, return to expr 1.
It's this...(basically swapping my order of steps 4 and 5)
First: (i = 1, square = 1, odd = 3)
Second: (i <= n)
Third: compiler was printf'ing
Fourth: compiler reads (square += odd)
Fifth: compiler reads (odd += 2, ++i)
Sixth: All values have been modified, return to expr 1.
///////////This is what I was writing before, "it hit me".///////////////////////
////you can ignore it if you like///////////
I'm reading or computing this wrong.
(i = 1, square =1, odd =3; i <= n; odd += 2, ++i)
Reading from left to right
I've got my i value
I've got my square value.
I've got my odd value of 3
I check and i <= n is true, so I continue, I print the loop body now, because the controlling expression has been validated as true, i is in fact <=n.
after printing the loop body, I return to expr 3 in the for loop statement above
I add 2 to odd, odd is now 5
I increment i, i is now 2