# A simple calculation of -- ((-y--)

jack parker
Ranch Hand
Posts: 43

i wanna ask why x=1? and y= -2?
thanks a lot!!

Rob Spoor
Sheriff
Posts: 20707
68
It's all a matter of operator precedence, and the way the postfix decrement operator works. y-- decreases y but returns the old value of y. So y-- returns -1, then decreases y to -2. That -1 is then negated to 1.

jack parker
Ranch Hand
Posts: 43
Rob Spoor wrote:It's all a matter of operator precedence, and the way the postfix decrement operator works. y-- decreases y but returns the old value of y. So y-- returns -1, then decreases y to -2. That -1 is then negated to 1.

i am sorry but why it is not do the -- first y=y-1 then y=-2 and times the - , become 2
thanks...

fred rosenberger
lowercase baba
Bartender
Posts: 12228
36
because all operators have a precedence. Just like in the equation "2 + 3 * 4", it is understood that multiplication comes before division, it is understood that the negation operator will be applied before the post-fix decrement operator.

Stephan van Hulst
Bartender
Posts: 6476
83
Actually, it is not a matter of precedence, but as Rob has already explained, it's simply how the postfix-decrement works. It decrements the variable, and returns the old value. If that's not what you want, then don't use it.

jack parker
Ranch Hand
Posts: 43
Stephan van Hulst wrote:Actually, it is not a matter of precedence, but as Rob has already explained, it's simply how the postfix-decrement works. It decrements the variable, and returns the old value. If that's not what you want, then don't use it.

i got so confused here, then when will it decrease 1 ? it always returns the old value? is that mean the -- is ignored in x?
and how y gets -2
thanks a lot!

Matthew Brown
Bartender
Posts: 4568
9
jack parker wrote:i got so confused here, then when will it decrease 1 ? it always returns the old value? is that mean the -- is ignored in x?
and how y gets -2

Are you sure the code you've posted is the correct code? Because there's no -- with x. In fact, there's no x variable declared or used anywhere.

y gets to -2 simply because you set it to -1, and then decrease it by one.

John Jai
Rancher
Posts: 1776

read like -(y--). The effect of the post fix operator is felt once the statement that has the post fix operator is executed. So in this line though postfix operation is applied, the effect do not take place.

So y is still -1 in this statement and a negation makes the value of 1 to be printed near x.

There is a semicolon at the last that ends the statement. Now the effect is seen and y changes to -2.

Next statement prints y's value that's -2.

Henry Wong
author
Marshal
Posts: 21719
85
jack parker wrote:i got so confused here, then when will it decrease 1 ? it always returns the old value? is that mean the -- is ignored in x?
and how y gets -2
thanks a lot!

There is a difference between the variable and the expression. For example....

y = x--;

The variable x does get decremented, however, the "x--" expression returns the old value, and hence, the variable y is assigned the old variable.

Having the expression be the old value doesn't mean that the variable isn't decremented.

Henry