• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logical operator precedence.

 
Sama Willson
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I was just going through some of Twist in Tale questions from the Mala Gupta book. My question is about logical operator precedence. To be more specific take for example this snippet of code:



In the explanation for line 9, Mala explains that "Because the short-circuit operator
AND has higher operator precedence than the short-circuit operator OR, the expression
is evaluated as follows:
(a >= 99 || (a <= 33 && b == 10))".

Ok, so far so good. But then I wrote a sample code snippet of my own:


The output for this code is: 11 20 30. My assumption that the line would be evaluated as : (a++==10 ||( ++b >20 && c++>40)) was obviously incorrect because neither b or c was incremented. Why is that? How do I know in which scenario && will take a higher precedence and in which situation it won't?

Thanks in advance for helping.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sama Willson wrote:The output for this code is: 11 20 30. My assumption that the line would be evaluated as : (a++==10 ||( ++b >20 && c++>40)) was obviously incorrect because neither b or c was incremented. Why is that? How do I know in which scenario && will take a higher precedence and in which situation it won't?

The output "11 20 30" confirms that your assumption about the evaluation order of the expression a++==10 || ++b >20 && c++>40 is correct. Because a++==10 evaluates to true, the short-circuit operator won't execute the right-hand side (++b >20 && c++>40) and therefore b and c are not incremented.

If the expression a++==10 || ++b >20 && c++>40 would have been evaluated like (a++==10 || ++b >20) && c++>40, then c would be incremented as well.

Hope it helps!
Kind regards,
Roel
 
Sama Willson
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel

I am still confused about it, in Mala's example, she says that since AND has higher precedence than OR, the AND operation would be performed first. So as far as I understand this, it should be something like: 2+3*3  where 3*3 would be evaluated first.  Keeping this in mind, in my expression wouldn't the AND expression be evaluated first ? Is there no such precedence between AND and OR? Am I understanding it incorrectly?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sama Willson wrote:I am still confused about it, in Mala's example, she says that since AND has higher precedence than OR, the AND operation would be performed first. So as far as I understand this, it should be something like: 2+3*3  where 3*3 would be evaluated first.  Keeping this in mind, in my expression wouldn't the AND expression be evaluated first ? Is there no such precedence between AND and OR? Am I understanding it incorrectly?

I just had a look at the example you are referring to and the study guide is completely wrong! That's not at all how this expression is evaluated. This is an errata item which should definitely be reported.

It is correct that the expression a++==10 || ++b >20 && c++>40 is equivalent with a++==10 || (++b >20 && c++>40) and thus will produce the same output (and same values for the different variables). But an expression (in Java) is always evaluated from left to right (except an assignment which is evaluated from right to left). So that means the sub-expression a++==10 is evaluated first, and because this sub-expression evaluates to true, the short-circuit operator won't execute the right-hand side operand anymore because the expression will always evaluate to true. And the output of your own code snippet illustrates that this order of execution is the only correct one. If it would have been evaluated like described in the book, you would have got different values for b and c.

In this topic you'll find an extensive explanation about the order of evaluation of expressions with short-circuit operators using a boatload of code examples. Definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic