Professional Documents
Culture Documents
HOME
INSTALL
GETTING STARTED
LEARNING
DOCS
BLOG
PACKAGES
Basic operators
In the previous chapter, we saw Elixir provides +, -, *, / as arithmetic
operators, plus the functions div/2 and rem/2 for integer division and
remainder.
G ETTI N G ST A RTED
1. Introduction
Elixir also provides ++ and -- to manipulate lists:
2. Basic types
3. Basic operators
4. Pattern matching
5. case, cond and if
6. Binaries, strings and char
lists
9. Recursion
10. Enumerables and streams
11. Processes
Elixir also provides three boolean operators: or, and and not. These
operators are strict in the sense that they expect a boolean ( true or false) as
their first argument:
15. Structs
16. Protocols
17. Comprehensions
18. Sigils
or and and are short-circuit operators. They only execute the right side if the
M IX A N D OTP
1. Introduction to Mix
2. Agent
http://elixir-lang.org/getting-started/basic-operators.html
1/4
3/8/2016
3. GenServer
4. Supervisor and Application
5. ETS
6. Dependencies and umbrella
apps
# or
iex> 1 || true
1
iex> false || 11
11
# and
S PON SORS
EL IX I R RA D AR
false
iex> !1
false
iex> !nil
true
As a rule of thumb, use and, or and not when you are expecting booleans. If
any of the arguments are non-boolean, use &&, || and !.
Plataformatec. Subscribe
below.
Elixir
Radar
weekly
Elixir also provides ==, !=, ===, !==, <=, >=, < and > as comparison
Subscribe
now
newsletter
operators:
iex> 1 == 1
true
iex> 1 != 2
true
iex> 1 < 2
true
http://elixir-lang.org/getting-started/basic-operators.html
2/4
3/8/2016
The difference between == and === is that the latter is more strict when
comparing integers and floats:
iex> 1 == 1.0
true
iex> 1 === 1.0
false
number < atom < reference < functions < port < pid < tuple <
maps < list < bitstring
You dont actually need to memorize this ordering, but it is important just to
know an order exists.
Operator table
Although we have learned only a handful of operators so far, we present below
the complete operator table for Elixir ordered from higher to lower precedence
for reference:
OPERATOR
ASSOCIATIVITY
Unary
Left to right
+ - ! ^ not ~~~
Unary
* /
Left to right
+ -
Left to right
http://elixir-lang.org/getting-started/basic-operators.html
3/4
3/8/2016
++ -- .. <>
Right to left
in
Left to right
Left to right
<|>
Left to right
== != =~ === !==
Left to right
Left to right
|| ||| or
Left to right
Right to left
=>
Right to left
Right to left
::
Right to left
when
Right to left
<-, \\
Left to right
&
Unary
Top
Next
http://elixir-lang.org/getting-started/basic-operators.html
4/4