Skip to content

Latest commit

 

History

History
190 lines (121 loc) · 4.97 KB

comparison_operators.livemd

File metadata and controls

190 lines (121 loc) · 4.97 KB

Comparison Operators

Mix.install([
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:tested_cell, github: "brooklinjazz/tested_cell"},
  {:utils, path: "#{__DIR__}/../utils"}
])

Navigation

Return Home Report An Issue

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.

Comparison Operators

Comparison operators allow us to compare values.

Using comparison operators, we can check if values are equal ==, greater than >, less than <, greater than or equal >=, and less than or equal <=.

Comparison operators can be used on any value but are most commonly used to compare integers and floats.

The result of a comparison is a boolean, either true or false.

For example, if we check that 5 equals 5, we return true.

5 == 5

Remembering Greater Than and Less Than

> greater than

< less than

To help remember which symbol < or > means greater than or less than, you might find it helpful to remember that the statement is true if the larger number goes on the larger side.

Some people find it helpful to visualize the statement as an alligator eating. And the alligator always wants the biggest meal.

# aligator wants biggest meal. The biggest number is `10` so return true.
10 > 2
# aligator wants biggest meal. The biggest number is `10`, but it eats `2` so return false.
10 < 2

Strictly Equals

There are two operators for checking equality in Elixir. === will check if two values are strictly equal in both value and type. So despite having the same numerical value, 1 does not equal 1.0 because integers and floats are not the same types.

1 === 1.0

However, if you only care about the numerical value and not the data type, you can use only two equals signs instead of three ==

1.0 == 1

Your Turn

Using comparison operators, determine if 10 + 10 * 15 is greater than (10 + 10) * 15.

Example solution
10 + 10 * 15 > (10 + 10) * 15

Using comparisons operators, determine if 4 ** 6 is equal to 4 * 4 * 4 * 4 * 4 * 4.

Example solution
4 ** 6 == 4 * 4 * 4 * 4 * 4 * 4

Using comparison operators, determine if 100 / 2 is strictly equal to 50.

Example solution
100 / 2 === 50

Comparing Different Data Types

You can compare different data types to each other in the following Sorting Order.

number < atom < reference < function < port < pid < tuple < map < list < bitstring

You'll notice several data types that you may not be familiar with above. Don't worry. There's no need to memorize this sorting order nor know all of these data types.

Comparisons Strings

When comparing strings, they compare based on alphabetical order. Letters earlier in the alphabet are evaluated as smaller than letters later in the alphabet.

"a" < "z"

Capital letters are always less than lowercase letters regardless of alphabetical order.

"Z" < "a"

Your Turn

Use comparison operators to determine if "hello" is equal to "hello".

Example solution
"hello" == "hello"

Commit Your Progress

Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish comparison operators section"

Up Next

Previous Next
Booleans Match Operator