Elixir - Comparision Operators



The comparison Operators in Elixir are mostly common to those provided in most other languages. The following table sums up comparison operators in Elixir. Assume variable A holds 10 and variable B holds 20, then −

Operator Description Example
== Checks if value on left is equal to value on right(Type casts values if they are not the same type). A == B will give false
!= Checks if value on left is not equal to value on right. A != B will give true
=== Checks if type of value on left equals type of value on right, if yes then check the same for value. A === B will give false
!== Same as above but checks for inequality instead of equality. A !== B will give true
> Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. A > B will give false
< Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. A < B will give true
>= Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. A >= B will give false
<= Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true. A <= B will give true

Example

Try the following code to understand all arithmetic operators in Elixir.

a = 10
b = 20

IO.puts("a == b " <> to_string(a == b))

IO.puts("a != b " <> to_string(a != b))

IO.puts("a === b " <> to_string(a === b))

IO.puts("a !== b" <> to_string(a !== b))

IO.puts("a > b " <> to_string(a > b))

IO.puts("a < b " <> to_string(a < b))

IO.puts("a >= b " <> to_string(a >= b))

IO.puts("a <= b " <> to_string(a <= b))

When running above program, it produces following result −

a == b false
a != b true
a === b false
a !== b true
a > b false
a < b true
a >= b false
a <= b true
elixir_operators.htm
Advertisements