• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt related to post-increment operators

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

This is a question from practice exam - 3 of Devaka's Examlabs.

These are only the first 2 lines.

int x = 90;

x = x++ + x--;

This line produces the output as 91 [value of x].
As per my understanding,the vakue of x should be [45+45 = 90] and then the post-increment operators should be evaluated.
Can anyone explain how the value of x evaluates to be 91 here?

Thanks,
Aditi.
 
Aditi Roychoudhury
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry,the two lines are as follows:-

int x = 45;

x = x++ + x--;
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


x = x++ + x--;

i brake it down as.
x = (x++) + (x--); x = 45
x = (45) + (x--); x = 46
x = (45) + (46); x = 45;
x = 91

post-increment means you use the current value of the variable first, then the variable value changes. I think you get that.
But in long statements, the statement is evaluated from left to right; rather than evaluate all the variables first, then the post-increments and pre -increments. Therefore, the variable value, x, is changing even as this statement is completing.

x = x++ + --x; = 90

\
hope that helps
 
Aditi Roychoudhury
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.
I had the understanding that the post-increment/decrement operators are evaluated only after the complete expression is evaluated and the value is returned.
Can someone please confirm this?

Thanks,
Aditi.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aditi Roychoudhury wrote:Thanks for the reply.
I had the understanding that the post-increment/decrement operators are evaluated only after the complete expression is evaluated and the value is returned.
Can someone please confirm this?

Thanks,
Aditi.


Aditi,

These are the fundamental rules regarding expressions with binary operators:
- Each operand in a binary operator must be fully evaluated before the operator acts on the operands.
- Operators are evaluated left to right (first the left operator, then the second operator.)

In your case, you have this:

x = x++ + x--;

You have quite a bit going on here, despite the apparent simplicity:
- An assignment operator.
- A numeric addition operator.
- A postincrement operator.
- A postdecrement operator.

The way this is evaluated is:
Before the assignment can take place, both the LHS (x) and the RHS (x++ + x--) must be evaluated.
For the RHS itself: both x++ and x-- must be evaluated before the addition can take place.
So x++ is evaluated (changes value of x to 46 and evaluates to 45,) x-- is evaluated (changes value of x to 45 and evaluates to 46,) and then the addition is performed (45 + 46, which equals 91.)
Then 91 is assigned to the variable x (the evaluation of the LHS of the assignment operator.

Analyzing it this way might seem overkill, but if you are systematic you will be able to reach sounder conclusions, especially with expressions that contain side effects like this one (or even more complex expressions.)
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a good question from Sierra and Bates SCJP Study Guide.
Chapter 4, Question 6.


The value printed is 4.

Index will be 3 after the println() statement.
 
Aditi Roychoudhury
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your replies.My confusion is now clear.

Thanks,
Aditi.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic