+ 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.

6th Jan 2016, 7:47 PM
Geoff williams
Geoff williams - avatar
8 Antworten
+ 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.
9th Jan 2016, 9:12 PM
CoJo
CoJo - avatar
+ 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.
28th Jan 2016, 12:17 PM
Smit Kiri
Smit Kiri - avatar
0
No... Both can change the value... Pre-increment first increments the value and assign and post-increment assign the value and then increments
13th Feb 2016, 1:41 PM
Ashwin Kasbe
Ashwin Kasbe - avatar
0
value is changed both time but prefix first increase value than uses variable and vice versa for post fix
28th Feb 2016, 5:35 PM
Sambhav Jain
Sambhav Jain - avatar
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 :-)
17th Apr 2016, 11:56 AM
Umakanth V S
Umakanth V S - avatar
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
24th Apr 2016, 12:58 PM
AK Musa
AK Musa - avatar
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 .
20th Jun 2016, 1:13 AM
Utpal Kumar
Utpal Kumar - avatar
0
here x++ increments when next x comes....but,++x increments at that instant or at that time...that's it..
17th Oct 2016, 5:21 AM
bhogavilli sai surendra