• 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

complex boolean expressions without parentheses

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody please tell me how boolean expressions like

if (b1 & b2 | b3)
if (b1 | b2 & b3 & b4)

are evaluated (I mean, without parentheses to group the variables)?

Thanks
 
author
Posts: 23951
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
Evaluation order is based on the operators precedence and the associatively. In this case, the AND operator has a slightly higher precedence than the OR operator. And they both have left-right assoc. So...

if (b1 & b2 | b3) ===> if ((b1 & b2) | b3)

if (b1 | b2 & b3 & b4) ===> if (b1 | ((b2 & b3) & b4))

Henry
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The order of boolean operator precedence is

0 !
1&
2^
3|
4&&
5||

Hope that helps
 
Sigrid Kajdan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both very much! This was really help.
In fact, I somehow seemed to think that && and || had the same operator precedence...
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the older versions of the exam, understanding these kinds of precedence rules was important. On the newer versions we've just added parentheses so that you won't have to memorize this stuff. If you encounter mock exam questions that test you on precedence, just know that that's an old style question and not too valid anymore.

hth,

Bert

p.s. That said, the short circuit operators are STILL important to understand
 
Beware the other head of science - it bites! Nibble on this message:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic