Lesson 7 of 16

Conditionals

if/else

Elixir's if is an expression that returns a value:

x = 15
label = if x > 10 do
  "big"
else
  "small"
end
IO.puts(label)   # big

cond

cond checks multiple conditions in order, like a chain of if/else if:

score = 82
grade = cond do
  score >= 90 -> "A"
  score >= 80 -> "B"
  score >= 70 -> "C"
  true -> "F"
end
IO.puts(grade)   # B

The final true -> is the catch-all clause.

Your Turn

  1. If x = 15, use if to print "big" (x > 10) or "small"
  2. If score = 82, use cond to print the letter grade ("B")
JS Transpiler loading...
Loading...
Click "Run" to execute your code.