Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Comparison Operators

comparison_operators.livemd

Comparison Operators

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

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

Be aware that the font of this lesson may shorten some operators.

  • === is ===
  • >= is >=
  • <= is <=
  • == ==

Here are a few example comparisons and their results.

Utils.table(:comparison_examples)

Comparing 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"

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.

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.

"" > 1

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

In the form below, enter the expected result of the comparison, either true or false.

Utils.form(:comparison_operators_result)

In the form below, Enter the operator ==, ===, >, <, >=, or <= that would make the statement true if it replaced all of the blanks __.

Utils.form(:comparison_operators)

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 comparison operators section"