I got the 3 of them wrong on my first try, and I can’t seem to grasp why Booleans 1, 2 & 3 are “True”, “False” & “True”. Can somebody help me out please?
AND
will only return true
when all (in this case, both) operands yield true
.
OR
will only return false
when all the operands yield false
.
(3 < 4 || false) => true
(false || true) => true
true && true => true
When reading an OR
expression, look for the one operand that stands out as true
and ignore the others.
3 < 4 => true
true => true
An OR expression is said to short-circuit on true, while an AND expression is said to short-circuit on false. In other words,
A || B needs to be all false to be false
A && B needs to be all true to be true
Testing this further…
!true && (!true || 100 != 5**2)
!true && ...
The first operand says it all… false. No point even evaluating any further (the computer doesn’t).
true || !(true || false)
Again, the first operand says it all… true. The rest can be ignored.
In order to spot this sort of logic we need to be intimately aware of boolean expressions and their operators so we can tell on sight what is happening. This is meant be all brain mechanics at play.
With that understanding in place we can go back to the example in the lesson,
(x && (y || w)) && z
and see in an instant that one of y
or w
must be true, along with x
and z
or the whole thing is false.
Perfect, thank you so much!
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.