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

Comparison Operators

reading/comparison_operators.livemd

Comparison Operators

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue BooleansMatch Operator

Review Questions

Upon completing this lesson, a student should be able to answer the following questions.

  • What are comparison operators, and what do they commonly compare?
  • How might we use comparison operators in our programs?

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
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
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
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"
"hello" == "hello"

Further Reading

Consider the following resource(s) to deepen your understanding of the topic.

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Comparison Operators reading"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue BooleansMatch Operator