• Post Reply Bookmark Topic Watch Topic
  • New Topic

A simple calculation of -- ((-y--)  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i wanna ask why x=1? and y= -2?
thanks a lot!!
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!