programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

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

Ranch Hand
Posts: 43

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

Sheriff
Posts: 21135
87
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...

lowercase baba
Bartender
Posts: 12565
49
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.

Saloon Keeper
Posts: 7993
143
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!

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.

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.

author
Sheriff
Posts: 23295
125
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

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?