- Explore the Truth Table & the Precedence List
- Practice expressing and evaluating complex conditions
- Practice making parse trees
Comparisons are often combined. Combinations can take one of two forms, && (and) and || (or). When you combine with and, both comparisons must be true
for the entire combination to be true
. By combining with or
, when either of the comparisons are true
, the entire combination is true
:
Imagine you need your morning coffee and a good night's sleep to have energy. This is an && (and) example. Let's consider different senarios.
Now imagine you need either an apple or a banana in the afternoon for a snack to feel full. This is an || (or) example. Let's again consider the different scenarios.
true && true
true && false
false && true
false && false
true || true
true || false
false || true
false || false
Use irb to check your answers or look at the answers hidden here.
true && true # => true and true is true
true && false # => true and false is false
false && true # => false and true is false
false && false # => false and false is false
true || true # => true or true is true
true || false # => true or false is true
false || true # => false or true is true
false || false # => false or false is false
Another way to write this is:
AND | true | false |
---|---|---|
true | true | false |
false | false | false |
OR | true | false |
---|---|---|
true | true | true |
false | true | false |
Compound comparisons often involve a chain of expressions to be evaluated. Ruby follows strict rules when deciding the order in which expressions are evaluated. These rules can be expressed in terms of their precedence. Operations with a higher precedence are evaluated before operations with lower precedence. To change the order in which operations are evaluated, add parenthesis ()
around the operations you want evaluated first.
Here is an abbreviated Precedence List from highest to lowest precedence:
- (things inside parentheses)
!
,unary +
**
unary -
*
,/
,%
+
,-
>
,>=
,<
,<=
<=>
,==
,===
&&
||
Note: unary + and unary - here means assigning a numeric value (Integer or Float) as either positive or negative, e.g. -5
, -12.2
, +30
and +2.0
. Thus a number with a unary + is functionaly the same as a number without it. (It doesn't actually do anything.)
When evaluating a complex compound conditional expression, it is nice to visualize the resulting expression. One way to do this is by using a parse tree. A parse tree will show the way the result of higher precedence operators are passed to later operations, until you have a result.
Here is an example parse tree for the expression 4 + 1 % 2 > 2 * 3 && true
Now create a parse tree for the expression 5 - 2**3 < 0 && (true || false)
Compare your solution with you neighbor.
With your neighbor(s), determine the final boolean
output of these compound conditions. Refer to the Truth Table and Precedence List in the textbook resource.
When in doubt, write down your work on paper.
# let's do these first three together
puts 5 > 4 && false
puts true && 5 * 2 > 3 + 3 * 2
puts true && 5 * 2 > (3 + 3) * 2
puts true && true || false
puts true && (true || false)
puts false && false || true
puts false && (false || true)
puts (false && false) && false && (true || false) || false
puts 4 == "4"
puts 4 == "4" || 4 == 4
puts 10 % 3 == 10.0 % 3
puts 10 * (5 / 2.0) == 10.0 * (5 / 2)
puts 10 * 5 / 2 > 10 * (5 / 2)
puts 2 * 2 ** 3 == (2 * 2) ** 3
puts (10 - 4) < +6 || -(2 * -4) > 0