This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help to Understand How Increment and Decrement works  RSS feed

 
rayan alex
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Please help to understand this out put of this



I counted the output as 35. But correct out put 33. Please help to understand.

This is how i counted.

++x uses incremented value. So first ++x = 11
next ++x increment x again. so second ++x = 12
next x++ uses the current value in the memory. so uses 12. 11 + 12 + 12 = 35


Thank you
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are telling first there is ++x then there is ++x and finally x++, so the way you have written your expression is incorrect.

So instead of this
write your expression neatly i.e.


you will surely get the output as 35.
 
rayan alex
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you for your reply. But correct out put is 33. I tried to say im getting 35.
I trying to figure out why i'm not getting 33 with my calculation.
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rayan alex wrote:


I counted the output as 35. But correct out put 33. Please help to understand.

This is how i counted.

++x uses incremented value. So first ++x = 11
next ++x increment x again. so second ++x = 12
next x++ uses the current value in the memory. so uses 12. 11 + 12 + 12 = 35


There is no "next ++x" (ie. second pre-increment). There is a space between the two plus signs. It is not a pre-increment, it is two unary plus operators. So, the correct expressions with precedence (and associativity) applied should be...



Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!