This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operator precedence

 
d jones
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In operator precedence the postfix operator apparently has a higher priority than the additive operators. However, if we have:

int c = 10;
int d = 5 + c++;
d equates to 15 - which is what I would expect because the postfix operator is only applied when the entire expression has been resolved.

I don't understand why then the postfix operators come before the additive operators on the operator precedence list.

Could anyone explain?

Many Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem here is that c++ is evaluated first, but it returns the value of c before begin incremented.
Compare
int d = 5 + (c++); // 5 + 10
and
int d = 5 + (++c); // 5 + 15

Same as this :
http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment
 
kwan Jang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int d = 5 + (++c); // 5 + 15?
why?
not int d = 5 + (++c); // 5 + 11 ???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typo. Thanks for pointing it out kwan.
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although increment operators come higher up in the hierarchy of precendence table, the order of evaluation also matters. for unary increment operators, I suppose the order of evaluation is from Right to Left.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic