This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Remember Postincrement operator increments the value only after the execution of the current statement (in this case - y = x++ + ++x; )
Before the statement, x is -1 and y is 0.
So as per the explanation above the code must translate to:
y should not get the value from x++ (thats what I thought) but it does.
In fact it behaves as if the code was
That is the only way y can end up with 1 else it should be -1.
May be I am missing some concept here/I might be confused but that is how it works.
I am guessing this has something to do with priority while evaluating an expression. I think ++ has higher priority than +.
Any references will be helpful.
Thanks in advance for any help.
Joined: Jul 29, 2009
Whenever you say
This will make x=1 & y=0
will make x=1 & y=1
In the scenario mentioned above, Pre Increment will be evaluated first, and post increment evaluated last based on precedence so the expression becomes
Therefore y=0, x=1 in the method "myMethod"
Sunny X Narula
Joined: Dec 07, 2008
Thanks for that Akanksha
Its clear now.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com