Boolean Operators: Precedence


#1

I don’t quite get how the code statement on this lesson cab be printed out false when all of the boolean operators are true. it makes no sense. Can someone please enlighten me?

The example: System.out.println( !( 1 < 8 ) || ( 5 > 2 && 3 > 5));

How does this print out as false?


#2

Hopefully this helps break it down:

( !( 1 < 8 ) || ( 5 > 2 && 3 > 5))

=( !( True ) || ( True && False))
=( !( True ) || ( False))
=( !( True || False ))
=( !( True ))
=( False )

with OR, you’re looking for anything to be True
with AND, you’re looking for everything to be True


#3

I made a mistake, the last part of the code statement was 3 < 8. That’s why I’m confused


#4

I actually found the solution, the answer is that

( !( true ) || ( true && true )) is actually

=( !( true || true && true )) which means that

even though both cases for the || and && boolean operators are true they still follow the precedence of the ! boolean operator because they reside within the same parentheses of !( // ) , all technically the same ( // )

=( !( true || true && true ))
=( !( true))


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.