Help with reading Boolean operators (precedence)

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

Hi,
I’m asked for a statement that prints out false. The one below does, however I’m a little confused as to why.

I read it like this. Brackets first: (5 >2 && 3< 5) true and true = true. Second !(1<8 = false. So with the final boolean, false OR true = true. But the statement prints false.

Any help would be great

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/learn-java/lessons/conditionals-control-flow/exercises/boolean-operators-precedence?action=resume

<In what way does your code behave incorrectly? Include ALL error messages.>

```

boolean riddle = !( 1 < 8 || (5 > 2 && 3 < 5));

<do not remove the three backticks above>

You got: not ( true or (true and true) ) = not( true or true ) = not true = false

thanks for the quick reply. Your answer has helped me to read the booleans a little different. Namely ! as ‘not’ instead of a flat ‘true’ of ‘false’.

Do you have any pointers or online guides i could check out to get up to speed with these kind of expressions ?

Tips for using them would probably be to never trust that you remember the precedence correctly. Be liberal with your parenthesis, it’s less error prone and will be easier for others to read. As for online resources or guides, I’d simply google “Java logical operators”. There aren’t really that many of them and they pretty much all work as you’d expect.

1 Like