Booleans
Mix.install([
{:kino, github: "livebook-dev/kino", override: true},
{:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
{:vega_lite, "~> 0.1.4"},
{:kino_vega_lite, "~> 0.1.1"},
{:benchee, "~> 0.1"},
{:ecto, "~> 3.7"},
{:math, "~> 0.7.0"},
{:faker, "~> 0.17.0"},
{:utils, path: "#{__DIR__}/../utils"}
])
Navigation
Setup
Ensure you type the ea
keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.
Booleans
Booleans (BOO-LEE-ANS) are true
and false
values in your code. They are often useful for
representing conditions and triggering actions in your program.
true
false
As part of programming, you’ll often create branching paths. Essentially there are multiple things that could happen depending on certain conditions.
Let’s take an example and say we have a program that controls a light.
Either we can tell the light to power on
or power off
. In this case, on
would be true
and
off
would be false
.
flowchart LR
A[is_powered] --> true --> on
A[is_powered] --> false --> off
Your Turn
In the Elixir cell below, turn the power on in our light control program to see the light turn on!
Replace false
with true
.
is_powered = false
Utils.visual(:light_control, is_powered)
Boolean Operators
Boolean operators allow you to compare booleans and return a boolean based on the comparison.
The boolean operators are and
, or
, and not
.
-
and
checks if two booleans are trueflowchart LR A[true] --> C B[true] --> C C[and] --> D[true]
-
or
checks if one or more booleans are trueflowchart LR A[false] --> C B[true] --> C C[or] --> D[true]
-
not
flips the value of a boolean.flowchart LR not --> false --> true a[not] --> b[true] --> c[false]
By comparing booleans, you can create branching paths in your code depending on certain conditions.
For example, let’s say we’re building a weather app. This app might send morning notifications to users to let them know that it’s going to be a sunny day.
You might check that the user is subscribed, that it is morning, and that it is sunny.
flowchart LR
A[is subscribed] --> and
B[is morning] --> and
C[is sunny] --> and
and --> true
true --> D[send notification]
and --> false
false --> E[don't send notification]
Converted into Elixir pseudo code that might look like this:
is_morning and is_sunny and is_subscribed
Your Turn and
In the Elixir cell below, compare true and true
.
Compare true and false
.
Compare false and false
.
Your Turn or
In the Elixir cell below, compare false or true
.
Compare true or true
.
Compare false or false
.
Your Turn not
In the Elixir cell below, evaluate not true
.
Evaluate not false
.
Combining Boolean Operators
You can use not
, and
, and or
in combination.
Your Turn
In the Elixir cell below, compare true and not false
flowchart LR
1[true] --> 2[and]
3[not false] --> 4[true]
4 --> 2
2 --> 5[true]
Compare false or not true
Truthy and Falsy
Sometimes it can be helpful to check a value exists rather than checking a boolean is true. A value is truthy if it exists and falsy if it does not.
nil
and false
are falsy because we treat them as false
for the sake of comparison. All other values are truthy because we treat them as true
for the sake of comparison.
and
, or
, and not
will not work on non-boolean values. Instead, you can use
&&
, ||
, and !
.
-
The and
&&
operator checks if two values are truthy and returns the right-hand truthy value.flowchart LR truthy1 --> op["and (&&)"] truthy2 --> op op --> a[truthy2]
-
The or
||
operator checks if one value is truthy and returns the first truthy value.flowchart LR truthy1 --> op["or (||)"] falsy1 --> op op --> a[truthy1]
-
The not flips a falsy value to
true
and a truthy value tofalse
.flowchart LR not --> truthy --> false a[not] --> falsy --> b[true]
While &&
, ||
, and !
work with booleans, it’s conventional to use and
, or
, and not
for booleans, and &&
, ||
, and !
when you expect non-boolean values.
Your Turn
In the Elixir cell below, compare 7 && 8
Compare 7 || 8
.
Compare nil || 10
.
Compare true && 20
.
Compare 10 || "hello"
.
Evaluate !20
.
Evaluate !nil
.
Evaluate !!30
. Notice that you can use this to convert a truthy to true
and a falsy to false
.
This is useful if you ever need to convert some non-boolean data into a boolean.
Commit Your Progress
Run the following in your command line from the project folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish booleans section"