This week's book giveaway is in the Python forum.
We're giving away four copies of High Performance Python for Data Analytics and have Tiago Rodrigues Antao on-line!
See this thread for details.
Win a copy of High Performance Python for Data Analytics this week in the Python 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

postfix and prefix increment precedence in conditional expressions

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused with the following code:


Here is the output:


"Conditional 2" and "conditional 4" do not behave as I would expect according to the operator precedence
rules. If the unary operators have higher precedence than the equality operators, then why aren't num++
and ++num evaluated first when they are on the right side of the == operator?

I am especially confused by this because the Java Language Specification says:

15.7.1 Evaluate Left-Hand Operand First
The left-hand operand of a binary operator appears to be fully evaluated before
any part of the right-hand operand is evaluated.



I'm sure I'm probably taking this out of context, but in "conditional 2" and "conditional 4", the left-hand
operands certainly seem to be evaluated first. If left-hand operands of binary operators are evaluated
first, what is the point of the higher precedence of unary operators? Are the unary operators applied
first seperately on each side of the binary operator, recusrively in some sort of tree structure. That seems
rediculously confusing. I know this is supposed to be very simple, but I can't seem to get past the apparent
contradiction. Thanks in advance for any help.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



In conditional 2,
Left hand side is evaluated first and then the right hand side is evaluated then the equality operator checks for equality
postfix operator first returns the value of num i.e 10 then the equality operator runs
before the equality operator runs the value is incremented but the value returned is the 10



In conditional 4,
Same : Left hand side is evaluated first and then the right hand side is evaluated then the equality operator checks for equality.
But the prefix operator first increments the value and returns it so ++num evaluates to 11 and then equality operator returns false.

Basically the precedence is still maintained but the behaviour of prefix and postfix;
says that postfix returns the original value first which is used in the evaluation of the expression and then increments and prefix increments the value first and then the new value is used in the expression.

I hope this helps

Regards,

 
Marshal
Posts: 71769
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you have been given a correct explanation, but I hope this will teach you not to write illegible code.
 
Greg Stevens
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for walking through the examples. What I don't understand is what does it mean for the unary operators to have higher
precedence than the equality operators if the are not evaluated first.

In other words

num = 10
num == num++

shouldn't num++ be evaluated first (evaluating to 10, but incrementing num to 11), then when num on the left is evaluated it
would be 11. 11 == 10: false
 
Greg Stevens
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but I hope this will teach you not to write illegible code



I would never write code like that. I have definitely learned not to use pre or post increment in an expression in which the variable occurs
more than once.

I see that the compiler gives the answer it does. Actually, my problem is that that I am studying in a class and the instructor is teaching that
the rules of precedence apply this way (obviously incorrect). I think he is confused. I am trying to figure out what is correct.

 
Campbell Ritchie
Marshal
Posts: 71769
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Operators are evaluated from left to right. Look at this Java™ Language Specification section. Note it says:

The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

Your example has num which is 10 to the left of the ==, then you go to the ==, but before the == is evaluated you execute the ++. Now num++ is still 10, so it returns true. It doesn't go back to the num with no operator on the left and alter that. That would be right-to-left evaluation.
 
author
Posts: 23893
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Stevens wrote:Thank you for walking through the examples. What I don't understand is what does it mean for the unary operators to have higher
precedence than the equality operators if the are not evaluated first.

In other words

num = 10
num == num++

shouldn't num++ be evaluated first (evaluating to 10, but incrementing num to 11), then when num on the left is evaluated it
would be 11. 11 == 10: false



Precedence and Evaluation Order are not the same thing. Precedence and Evaluation Order are not the same thing. Precedence and Evaluation Order are not the same thing. See my explanation in your other topic.

https://coderanch.com/t/481607/Beginning-Java/java/yet-another-postfix-prefix-precedence

And BTW, did I also mentioned that Precedence and Evaluation Order are not the same thing?

Henry
 
Campbell Ritchie
Marshal
Posts: 71769
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you trying to tell us precedence and evaluation order are not the same thing, Henry Wong?
 
lowercase baba
Posts: 12955
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry - I'm sorry, but you're wrong.

Precedence and Evaluation Order are not the same thing.
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic