wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Please help to Understand How Increment and Decrement works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please help to Understand How Increment and Decrement works " Watch "Please help to Understand How Increment and Decrement works " New topic
Author

Please help to Understand How Increment and Decrement works

rayan alex
Ranch Hand

Joined: Nov 25, 2012
Posts: 33
    
    1
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

Joined: Feb 03, 2011
Posts: 247
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

Joined: Nov 25, 2012
Posts: 33
    
    1
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

Joined: Sep 28, 2004
Posts: 18505
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help to Understand How Increment and Decrement works
 
Similar Threads
doubts getting exponentially INCREMENTed
post increment operator
Be The JVM (Head First)
Please help me to figure out confusion with my program output.
Need Explanation of unpredicted output even if methods are synchronized