Procendence


#1



https://www.codecademy.com/en/courses/learn-java/lessons/conditionals-control-flow/exercises/boolean-operators-precedence


I don't understand how the line prints out false. I got it correct, but after working it out individually in my head I get true. Unless I'm doing the order wrong. Someone explain please.


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


#2

Lets work it out like an algebra problem

!( 1 < 8 and (5 > 2 or 3 < 5))
...1<8..and...5>2...or...3<5...
....true.........true.........true.
.....true..and..(true or true)...
.....true and true...
.......true....

BUT then there's the ! which is not. so the statement simplifies to
!(true)
and if something's not true its ___. Hope this helps you understand it!


#3

Let's try to break it down to the simplest components. Starting with,

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

We see NOT on the very outside, so we can strip this and set it aside for later.

!( expression )

where,

expression = 1 < 8 && (5 > 2 || 3 < 5)

There is a single logical operator (ignore the OR in brackets) which means two operands, so we can write,

expression = expr_A && expr_B

where,

expr_A = 1 < 8              // true
expr_B = (5 > 2 || 3 < 5)

so we can now write,

expression = true && expr+B

expr_B can be written as,

expr_B = aB || bB

where,

aB = 5 > 2                  // true
bB = 3 < 5                  // true

so we can now write,

expression = true && true   // true

and finally,

boolean riddle = !(true)    // false

#4

( 1 < 8 && ( 5 > 2 || 3 < 5))
     |  AND    |   OR   |
     v         v        v     
   true      true      true

The ( 5 > 2 || 3 < 5) results to true. This is because either 5 > 2 OR 3 < 5 should be true. Because 5 > 2 is true it doesn't matter if 3 < 5 is true or false, this works both ways ofcourse.

So then we have 1 < 8 AND true. Both are true so in total we get:
( 1 < 8 && ( 5 > 2 || 3 < 5)) is true.

But because you have the !, the not-operator, in front of the statement. It gives the opposite outcome, for example:

!true = false
!false = true

This makes that:

!( 1 < 8 && ( 5 > 2 || 3 < 5)) = false      
Because:
!(true) = false

Aaand there goes 10 minutes worth of typing because other people type faster ._.


#5

I think he's got enough explanation for another month...


#6

Supplemental

It should be noted the OR is short-circuiting meaning it exits on the first true operand. bB need never be evaluated.


#7

Thanks to everyone that replied. The breakdown's were very helpful!


#8

Wait what? You're already done reading? :stuck_out_tongue:


#9

Haha, I'm picking it up pretty quickly. It's just that this is my first coding language. I'm able to skim over it enough to understand it though :slight_smile:

The algebra reference that ghughes13 made was very helpful.


#10

Actually, if you don't mind, I have another question.

It said to do !, &&, | |, in that order.

Clearly not what happened here. What does that apply to?


#11

The only reason I didn't do it in that order is PEMDAS or order of operations. gotta do the stuff in the parenthasis first.


#12

The takeaway lesson here should be precedence, which is similar to algebraic order of operations, but the optimal term here.

Not to wave a flag, the example in post 3 shows the exact order, following precedence rules. The brackets are a very important part of the starting expression, and dictate precedence before we even begin to asses the operators.


#13

Why is ! done last?

The order is ! , && and | |

I understand it has something to do with brackets but I still don't understand can someone please explain.


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

The inner most brackets are tackled first.

( 5 > 2 || 3 < 5)

Only one of the operands needs to be true for a yield of true. In this case both are, but since the first one is true, the second one is never evaluated.

We still cannot apply NOT (!) since there are still brackets.

 !( 1 < 8 && true )
    -----    ----

Since the first operand is true, the result is true. That leaves,

! true

which toggles to false.

In Canadian schools a popular acronym in algebra is BEDMAS which can be shortened to BEMA since division is multiplication, and subtraction is addition.

Brackets
Exponents
Multiplication
Addition

In logical expressions, when there are no brackets,

! true || true && true

in the above, ! true is evaluated first.

false || true && true

Next, the and expression is evaluated

false || true

And we know what this will yield... true.