Need help on question 5: Boolean Operators Precendence


#1

I don't, at all, understand where the !, && and || are meant to be placed, is it inside the bracket or outside it?
And what do they even mean?


5/11 Boolean operators presedence
#2

These are Boolean Operators. They compare different items. For Example:

if (1 < 4 && 0 > 5)
else if (21 <= 19 || 17 >= 28)
else if ( !(true == true) )

In this case it is asking IF 1 is less than 4 AND 0 is greater than 5.
If both of those criteria are met then it will be a true statement.

&& is AND. It is checking to see if both statements are true.
|| is OR. It is checking to see if EITHER of them are true. Only 1 side has to be true for it to be a true statement.

! is the not Operator. It is used to negate statements that are true.


#3

Just for clarification, the not operator is for both true and false. !true = false, and !false = true.


#4

Correct. Sorry about being vague.


#6

@thebrokencode,
= https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Summary of Operators

The following quick reference summarizes the operators supported by the Java programming language.

Simple Assignment Operator

=       Simple assignment operator

Arithmetic Operators

+       Additive operator (also used
        for String concatenation)
-       Subtraction operator
*       Multiplication operator
/       Division operator
%       Remainder operator

Unary Operators

+       Unary plus operator; indicates
        positive value (numbers are 
        positive without this, however)
-       Unary minus operator; negates
        an expression
++      Increment operator; increments
        a value by 1
--      Decrement operator; decrements
        a value by 1
!       Logical complement operator;
        inverts the value of a boolean

Equality and Relational Operators

==      Equal to
!=      Not equal to
>       Greater than
>=      Greater than or equal to
<       Less than
<=      Less than or equal to


Conditional Operators

&&      Conditional-AND
||      Conditional-OR
?:      Ternary (shorthand for 
        if-then-else statement)

Type Comparison Operator

instanceof      Compares an object to 
                a specified type

Bitwise and Bit Shift Operators

~       Unary bitwise complement
<<      Signed left shift
>>      Signed right shift
>>>     Unsigned right shift
&       Bitwise AND
^       Bitwise exclusive OR
|       Bitwise inclusive OR

#9

None of these comments helped me, just confused me even more...


#10

The exercise has provided us with a variable, boolean riddle and has it set to a logical expression:

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

To evaluate this expression, we start reading from left to right. The NOT operator is first up, and it has precedence over the other operators except everything is enclosed in brackets so we must evaluate inside them, first. The NOT operator has still not been applied.

Inside the brackets are two operands in an OR operation. Again, one is in brackets so we must evaluate that to begin with. It consists of two operands in an AND operation. Finally something to evaluate:

5 > 2 && 3 < 5
true  &&  true    which yields `true`

So now we have,

1 < 8 || true

In an OR operation, either operand may be true. As long as one of them is, the expression yields true. Even if the above were to read,

1 > 8 || true

it would still yield true. That leaves us just the NOT operator now that all the brackets are gone through.

!(true)

The result in riddle, then, is false. Looking at the second logic puzzle,

// ( !true || (false && true) )

Here we can see that nothing is outside of the outer brackets so they can be removed, leaving us with the expression,

!true || (false && true)

There is still one operand in brackets, so it can be evaluated first. false && true is a contradiction, which in Boolean terms is always false as a result. Which gives,

!true || false

Evaluating the NOT on the first operand yields false so we have,

false || false  which yields `false`

So,

boolean puzzle = _____ ?

You guessed, it, false.


#11
!true || (false && true)

There is one more thing that we didn't illustrate, yet. What happens if we remove the remaining brackets, without evaluating?

!true || false && true

How do we evaluate this now there are no brackets?

Answer:

The NOT operation can be performed immediately

!true           => false    [1]

AND takes precedence to OR

false && true   => false    [2]

Now the OR

 [1]      [2]
false || false  => false

#12

Seems like you want easy answer. Do you understand that it's mean that someone will "do the math" for you? Ok, here is your easy answer: 5 > 2 == 6 < 12. Is it easy enough?


#13