Does the value of x=x-- evaluates as the original value of x or it increments by 1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Does the value of x=x-- evaluates as the original value of x or it increments by 1?

there is ambiguity on the outcome.

27th May 2017, 4:01 PM
Pacha
4 Answers
+ 2
It means: if x=10; now when x=x--; so put x original value into x(left hand side one) -- is a post decrementer here meaning first use it or assign then decreament but as we have used the value which is 10 so -- has no significance. If you did like this: x=--x; then you would get x equal to 9,in this case first subtract by 1 then use it or assign to left side x.
27th May 2017, 4:14 PM
Fahad
Fahad - avatar
+ 2
Let x here be 10 originally, then it will be easier to explain... a=b-- will mean that b's original value shall be passed to a and b is to be decremented by 1. So if a=2 and b=1, a=b-- makes b=0 and a=1. Now, expressions in C++ are always executed from left to right. and if there is an assignment, the whole expression is simplified to the end and then assigned... So when we try x=x--; x becomes x--, and so will become 9 later... But now, before it becomes 9, it is assigned to x. So x remains to be 10. Thus, printing x after this will give you 10 (assuming start at 10) no matter what. Though if you try x=--x, this will give a 9.
27th May 2017, 4:14 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
@Retr0 -- is decrement, ++ is increment.
27th May 2017, 4:40 PM
Rrestoring faith
Rrestoring faith - avatar
0
how will it increment with this sign -- (correct me if I am wrong I have forgot a little things about c++)
27th May 2017, 4:07 PM
_Retr0/-
_Retr0/- - avatar