Boolean operators: Presedence exercise 5


#1

So the assignment is: “Line 4 has a code statement that is incomplete. Use each Boolean operator no more than once to replace the empty comments /**/. The code statement should print out false.”

So i wrote it like this:

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

and that’s correct, this prints out ‘false’, but i don’t understand how.

First, 1 is less than 8 so the ! returns out opposite, which is false.
Then, 1 is less than 8 is true and 5 is larger than 2 is also true, so the && should print out true (?)
But then, 5 is bigger than 2 as well as 3 being less than 5, so the || should also print out true (?)

So how does all this print out false? When both && and || is true ?
What have i been mising?


#2

the ! operator changes true to false and false to true. It is a way of saying “not”
Notice it is outside all of the parenthesis


#3

Oh okay so the result of everything -inside- the paranthesis is true, but since the ! is outside it, it turns the total to false. Thanks!


#4

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