• Post Reply Bookmark Topic Watch Topic
  • New Topic

Precedences of && and ||  RSS feed

 
Antonio Michele
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Maybe it's a stupid question but since "&&" has a higher precedence than "||" why isn't my example incrementing j?



Thanks in advance!

Best regards
Proton
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because operator precedence doesn't affect evaluation order. Expressions are always evaluated left to right.
 
Antonio Michele
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick response.
So if boolean expressions are always evaluated from left to right, under which circumstances can
the higher precedence of "&&" take effect?
I couldn't think of anything else than my example and was wondering why they don't have the same precedence.

Best regards
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Antonio Michele wrote:
So if boolean expressions are always evaluated from left to right, under which circumstances can
the higher precedence of "&&" take effect?


The "&&" (logical AND) operator having higher precedence is in effect. Did you noticed that when the "||" (logical OR) operator short-circuited the right operand, it short-circuited both the other two conditionals? This is because the logical AND operator has higher precedence, and hence, the right operand of the logical OR operator included the rest of the expression, and had to be short-circuited together.

Henry
 
Antonio Michele
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but this one I don't get.
I noticed, that the other expressions were short-circuited, but I don't see why this is because of the higher precedence of "&&".

Best regards
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Antonio Michele wrote:Sorry, but this one I don't get.
I noticed, that the other expressions were short-circuited, but I don't see why this is because of the higher precedence of "&&".


Because the logical AND has higher precedence than the logical OR, this expression ...

is equivalent to this expression...

So, the right operand to the logical OR is ...

so, the short circuit will affect both the second and third conditionals.

If the logical AND did not have higher precedence, then only the second conditional would have been short-circuited. This means that the logical AND would have to be executed, as the left operand (to the logical AND) would have been true; and the result would have been false, as the right operand (third conditional) is false.

Henry
 
Antonio Michele
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a perfect explanation!

Thank you very much!

Best regards
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!