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
- If
x = 15, useifto print"big"(x > 10) or"small" - If
score = 82, usecondto print the letter grade ("B")
JS Transpiler loading...
Loading...
Click "Run" to execute your code.