✅ What's the difference between ++variable and variable++?
I understand the complier sees the compound variable operators differently depending if it's before or after the variable, but I'm not sure how. Can someone explain this to me like I'm five?
14 Replies
So with x++, y stays the same as x's original value, while x goes up by one?
What about z?
no x gets assigned to y first then gets incremented
while in z case x gets incremented first and then gets assigned to z
I will say, if understanding your code relies on understanding this distinction, you probably should change it.
I'm learning the basics on mslearn
I don't entirely understand, but thanks for the info
lets break it doen more
That helped a lot. I get the first part now
Like jcotton put above, the 2 is taken from the first part when x is redefined as 2 and then z becomes the same as your end comments?
Is that why z becomes the same?
++x
— increment, then use
x++
— use, then incrementThanks for the help everyone!
x=1;
y=0;
y = x++
means :
y = x;
x = x+1;
--------
y = ++x
means :
x=x+1;
y=x;
basically:
i++ means after iteration add 1
while ++i means before iteration add 1
what do you mean by iteration? if you're talking about using it in a loop, that's not true
prefix or postfix only affects the value returned by the expression, if you aren't using the return value (which the 3rd statement of a for loop doesn't, and neither does a standalone increment) they're equivalent
This seems to be solved, perhaps $close it.
If you have no further questions, please use /close to mark the forum thread as answered