Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Post- and Pre- unary operator precedence (Java OCA 8 Programmer I Study Guide)

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In chapter 2 on page 57 there is an example on the pre/post increment and decrement operators:



It explains that first, the x is incremented and returned:


However, on page 51, Table 2.1 it says that post-unary operators have higher precedence over pre-unary operators. So in this example why wouldn't the x be decremented to 2 first and return 3?



Then x would be incremented to 3



...and then x would be decremented back to 2:







This yields the same result that the book states, but just in a different manner. Which is correct?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alan Couze wrote:This yields the same result that the book states, but just in a different manner. Which is correct?


This topic has a great explanation about the same code snippet. If you still have doubts after reading this topic, let us know by simply hitting the "Post Reply" button

Hope it helps!
Kind regards,
Roel
 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it comes to pre- and post increment/decrement, the assign first or increment/decrement first needs to be clear.

Post increment / decrement (x++) is assign then increment/decrement

Pre increment / decrement (--x) is increment/ decrement then assign

Both explanations are correct. Either approach will get you the same result.

Using the rule above, working from left to right applying the post-unary or pre-unary all at once is probably the straightforward way, despite the post unary has higher precedence.

y = ++x * 5 / x-- + --x

using left to right post and pre unary apply all at once
=> 4 * 5 / 3 + 1
=> 7

or using post unary first
=> 3 * 5 / 3 + 2
=> 7
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

K. Tsang wrote:Both explanations are correct. Either approach will get you the same result.


That's absolutely NOT true! Expressions are evaluated from left to right, so post-unary operator is not executed first!

K. Tsang wrote:using left to right post and pre unary apply all at once
=> 4 * 5 / 3 + 1
=> 7


Those values are incorrect. Should be:
=> 4 * 5 / 4 + 2
=> 7
(the value of x will never be 1)

K. Tsang wrote:or using post unary first
=> 3 * 5 / 3 + 2
=> 7


And this is completely wrong as the post-unary operator is not executed first at all (as mentioned before).

With any other expression using post- and pre-unary operators, the result will not be the same. So you can't conclude that both explanations are correct just because for one expression the results are the same. Here is an example that disproves both explanations being correct!1/ Expression evaluated from left to right (the correct one): int j1 = 6 * 6 * 5; // i1 = 4, j1 = 180
2/ Post-unary operators evaluated first (the wrong one): int j1 = 4 * 5 * 4; // i1 = 4, j1 = 80

Hope it helps!
Kind regards,
Roel
 
Sheriff
Posts: 7602
521
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan,

You always should try in practice after you read, so you could check if you're right. How to check?

Here is how I think in my head about them ("++x", "x--", "--x"), like about a standalone expressions.

At the beginning you have x = 3.

1. x = 3

2. Since it is pre-increment, you increment "x" first (3+1), only then you place it in expression
int y = 4 * 5 / ...

3. As you know, your "x" is 4 now. Next step you have post-decrement (x--). So you first place your current "x" value to expression, then decrement.
int y = 4 * 5 / 4 + ...

4. You know from previous step. That your "x" after was placed to expression, then had to be decremented (4 - 1 = 3). So the last one you have is "--x". And current "x" is 3. So again, decrement first again, and only then place to expression.
int y = 4 * 5 / 4 + 2

5. And, since first multiplication/division, then addition, you should think about it as:
int y = ((4 * 5) / 4) + 2

So you have:
int y = 20 / 4 + 2;
int y = 5 + 2;
int y = 7;

Hope it helps a bit.
 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something worth noting is this kind of thing:

It will print 0.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Guillermo Ishi wrote:Something worth noting is this kind of thing:

It will print 0.


Indeed! And if you are looking for a detailed explanation why it prints 0, look no further, just click here
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic