+ 4
So x++ and ++x are essentially both the same as x=x+1 but the postfix is the only one that changes the value? Or no? I think I missed something.
8 odpowiedzi
+ 8
x++ will "use" or evaluate x before incrementing it. ++x will increment x before evaluating it.
So, take this block of code;
x = 1;
y = 2;
y += ++x;
By the end of this block, x = 2, and that value will have been added to y such that y = 4. However, if we'd used x++ instead, we would have y = 3. In that case, our line "y += x++;" would have added the value of x to y BEFORE incrementing it. Again, having the line be "y += ++x;" would add the value AFTER incrementing.
+ 1
Both of them changes the value. The difference is if ++x is used in an equation like y=++x + 3 and if x=1, then first x will become 2 and the equation is solved, so y will be 5. But if the equation is y=x++ + 3, then first the equation is solved, so y will be 4 and then x will be incremented.
0
No... Both can change the value...
Pre-increment first increments the value and assign and post-increment assign the value and then increments
0
value is changed both time but prefix first increase value than uses variable and vice versa for post fix
0
@Geoff,
Both increment, but WHEN the incrementing happens, is key.
In the case of ++x, the value of x is incremented FIRST, and then the value of whichever expression x is used in, is evaluated.
In the case of x++, the present value of x is used in the expression, and THEN the value of x is incremented.
Hope this helps :-)
0
They are the same essentially but that changes when in an expression.
x=5
y=++x
y=x++
When evaluated the first why display the value of x after increment while the second y displays value of x after increment( which is 6 and 5 respectively)
This is the way I understood things. if I am wrong I am open to learning. Thanks
0
There is a rule that expalin that a variable can not be upadated twice in an expression. if such things happen the result depends upon the implementation of that specific compiler .
0
here x++ increments when next x comes....but,++x increments at that instant or at that time...that's it..