0
C++ for loop question
int x=1; for (int i=3; i<5; i++){ x+=i; why it is 8, does anybody know it?
2 Answers
0
x starts at 1 (x=1)
i starts at 3 (int i = 3)
x += i means take the value of i, add it to x, then assign that value back into x. So with x = 1 and i = 3, 1 + 3 = 4
Increment i by one so now it is at 4.
Same as above, now x is 4 and i is 4, so 4 + 4 = 8
increment i so now it is 5
The loop condition is now broken, so the loop exits.
0
Thanks a lot man!



