+ 1
I still dont get the difference between x++ and ++x
If possible, include some examples for both
2 Answers
+ 1
x++ increases x's value by one. Often used like this
for (int x, x>=10, x++)
Every time the for loop is run, x's value is increased by one. If x started at 0 it will run 10 times.
++x would do the same thing in the for loop above, however in the following code, "++x" and "x++" do different things
int x=5
int y=5
y=++x
Outputs y=6 x=6
int x=5
int y=5
y=x++
Outputs y=5 x=6
This is because "y=++x" adds 1 to x and then assigns it to y, where "y=x++" assigns y to x and then increases x's value by 1
+ 1
ooh now it makes sense
thank you very much