# x++ , ++x

Brian Pianczk
Ranch Hand
Posts: 45
I am not sure why, what is going on here is not getting through to me.

Produces this output.

When ++y x = 2 and y = 2
When z++ a = 1 and z = 2

why?

Steve Luke
Bartender
Posts: 4181
22
++y is the pre-increment. x = ++y essentially says 'increment y, then use the new value of y to assign to x'

z++ is the post-increment. a = z++ essentially says 'use the current value of z to assign to a, then increment z'

Brian Pianczk
Ranch Hand
Posts: 45
Thanks, I find it very helpful to see it in regular ol English.

Campbell Ritchie
Sheriff
Posts: 50175
79
Please do a search; that sort of problem causes no end of confusion and there are lots of similar questions here on beginners'.

Brian Pianczk
Ranch Hand
Posts: 45
Campbell Ritchie wrote:Please do a search; that sort of problem causes no end of confusion and there are lots of similar questions here on beginners'.

I tried, but the search wont take ++ as a valid entry, and I didn't know exactly what else to look for.

Campbell Ritchie
Sheriff
Posts: 50175
79
Try searching for "preincrement postincrement" and you get a few hits: 1, 2 (end of discussion), 3, and there are others.