Skip to content

Latest commit

 

History

History
163 lines (122 loc) · 4.17 KB

boolean_expressions.md

File metadata and controls

163 lines (122 loc) · 4.17 KB

JumpStart Live (JSL)

Day 2

Boolean Expressions

Overview

A boolean expression is an expression that evaluates to either true or false. They are commonly used in if/elsif statements and while loops.

Precedence

  • Also commonly called order-of-operations
Priority Operation
1 parens
2 unary operations
3 multiplication, division, mod
4 addition, subtraction, string concatenation
5 less than, less than or equal to, greater than, greater than or equal to
6 equal to, not equal to
7 and
8 or

Relational Operators

  • Relational operators allow you to compare two values
  • In ruby you can use relational operators on numbers and strings
  • The opposite of > is <= and the opposite of < is >=
Operator Description Example Result
== equals 1 + 1 == 2 true
!= does not equal 3.2 != 2.5 true
< less than 10 < 5 false
> greater than 10 > 5 true
<= less than or equal to 126 <= 100 false
>= greater than or equal to 5.0 >= 5.0 true

Boolean values

  • The two possible values of a boolean data type are true and false
  • A non-boolean value that evaluates to true, is called "truthy"
    • Everything in ruby is "truthy" except for nil
  • A non-boolean value that evaluates to false, is called "falsey", also sometimes "falsy"
    • nil is the only "falsey" value in ruby

Examples

if 2
	puts "2 is truthy"
end

if 'hello'
	puts "\"hello\" is truthy"
end

if !nil
	puts "nil is falsey"
end

Logical operators

  • Logical operators allow you to combine or modify boolean expressions
Operator Description Example Result
&& and (2 == 3) && (-1 < 5) false
` ` or
! not !(2 == 3) true
p q p && q p || q
true true true true
true false false true
false true false true
false false false false
p !p
true false
false true

DeMorgan's Laws

Named after Augustus De Morgan, a 19th-century British mathematician, they are a set of rules that describe what happens when you perform a negation on a && statement, or an || statement.

  • !(p && q) can also be described as !p || !q
  • !(p || q) can also be described as !p && !q

Short Circuit Evaluation

Short Circuit Evaluation describes a semantic rule related to logical operators in boolean expressions. It states that the second argument is only evaluated if the first argument is not enough to determine the overall value of the expression.

Specifically, when the first argument of an && statement evaluates to false, the second argument is not considered since the entire statement will evaluate to false. Conversely, if the first argument of an || statement evaluates to true, the second argument is not considered since the entire statement will evaluate to true.

Examples
x = true
y = false

# no short circuit
# y is evaluated
if x && y
	puts "1"
end

# short circuit
# x is not evaluated
if y && x
	puts "2"
end

# short circuit
# y is not evaluated
if x || y
	puts "3"
end

# no short circuit
# x is evaluated
if y || x
	puts "4"
end

Common Mistake

It's important to understand how to combine relational operators with logical operators.

You cannot for example ...

x = 1
if x == false || 2 || 48
	puts "x is false, 2, or 48"
end

This code prints x is false, 2 or 48, unexpectedly.

x == false || 2 || 48
false || 2 || 48
2 || 48
# and since 2 is truthy, the statement is printed
# note that short circuit evaluation results in the 48 not being considered

The correct way to code this is ...

x = 1
if x == false || x == 2 || x == 48
	puts "x is false, 2, or 48"
end

Resources