• 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

Logical operators precedence

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please explain how to come up with this answer? It's from the Sierra and Bates book (page 199), but I still do not understand when looking at the answer explanation in the book. I guess I need someone to explain the order of evaluation of lines 6 and 8 in detail. Thanks!!
Given the following:
1. class SSBool {
2. public static void main(String [] args) {
3. boolean b1 = true;
4. boolean b2 = false;
5. boolean b3 = true;
6. if ( b1 & b2 | b2 & b3 | b2 )
7. System.out.println("ok ");
8. if ( b1 & b2 | b2 & b3 | b2 | b1 )
9. System.out.println("dokey");
10. }
11. }
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, what's the answer you're looking for help explaining?
I'll assume it's the output of the program.
Then, which part of the program are you looking for help explaining?
I'll assume it's the compound boolean conditions used in the two if statements.
You can think of the AND operators as binding operands more tightly than the associated OR operators. In other words, the AND operators are evaluated before the OR operators.
If it helps you, you could add parentheses to the expression to help clarify the order that things are evaluated.
So, for the first if statement, you could think of b1 & b2 | b2 & b3 | b2 as (b1 & b2) | (b2 & b3) | b2, and then hopefully it becomes clear that the expression evaluates to FALSE OR FALSE OR FALSE, which is FALSE, and so "ok " is not displayed to the console.
Is the evaluation of the second if statement any clearer?
Now, back on the subject of asking a good question. When asking folks a question, taking a minute to first explain things to your Cardboard Analyst and to think about Asking a Good Question is time well spent.
 
AJ Brown
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding the ( ) helps me understand
so the line....
if ( b1 & b2 | b2 & b3 | b2 | b1 )
should be read as
(true & false) | (false & true) | false | true
goes to next step...
false | false | false | true
so this is true then
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep.
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the same problem as you. But after a while, I figure out that I wasn't reading the explanations carefully enough. Either that or I wasn't really trying to think.
Anyway, &&, & operators have higher precedence than || or | operators. Meaning to say, you evaluate the operands that are Anded together first, then ORing the rest.
No parentheses are needed, if you follow their precedence order. However, putting the brackets in sure help to improve your code clarity! But then again, in the exams, they're out to confuse us!
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic