Conditions (if / else)
Decision making with if...do...else...end and logical operators.
Simple enough for a 5-year-old to understand
decision with if
age = 20if age >= 18 do IO.puts("Adult")else IO.puts("Child")endDecision making with if...do...else...end and logical operators.
Simple enough for a 5-year-old to understand
age = 20if age >= 18 do IO.puts("Adult")else IO.puts("Child")endConditional structures in Elixir are expressions and produce values. if, unless, cond, and case are used for this purpose. Which one you choose depends on what the branching is based on.
The truthiness rule is simple: only false and nil are falsey, everything else is truthy. Zero, an empty string, and an empty list are all considered truthy. This is different from many languages and leads to silent bugs if you aren't careful.
There is no "else if" structure in Elixir. When you want to try multiple conditions, you use cond; when you want to branch based on the shape of a value, you use case. Knowing the distinction between the two keeps your code readable.
If there is a single condition, if is enough; if multiple independent conditions are to be tried, cond is readable.
If you are branching based on the structure of a value, case is the right tool and allows you to unpack parts at the same time.
Wrong
sayi = 5 if sayi > 10 do IO.puts "big" else if sayi > 3 do IO.puts "medium" end
There is no else if structure in Elixir; this code results in a syntax error. To try multiple conditions, cond is used. Those who don't know the structure try this syntax out of habit from other languages, and the error message isn't very descriptive either.
Correct
sayi = 5 cond do sayi > 10 -> IO.puts "big" sayi > 3 -> IO.puts "medium" true -> IO.puts "small" end
Wrong
liste = [] if liste do IO.puts "full" end
An empty list is considered truthy in Elixir; the block runs and the wrong message is printed to the screen. Only false and nil are falsey. You need to write an explicit check to test for emptiness.
Correct
liste = [] if liste != [] do IO.puts "full" end
Wrong
sayi = 5 cond do sayi > 10 -> "big" sayi > 100 -> "very big" end
If no condition is met, the program stops with a CondClauseError; additionally, the second branch is logically never reached because the narrower condition was written later. A cond structure must always have a catch-all branch and conditions should be ordered from narrowest to widest.
Correct
sayi = 5 cond do sayi > 100 -> "very big" sayi > 10 -> "big" true -> "small" end
cond evaluates conditions and picks the first true one; case matches a value against patterns. If you are doing comparisons, cond is right; if you are looking at structure, case is right.
Yes, it returns the last expression of the executing branch. If no condition is met and there is no else branch, it returns a nil value. That's why you can directly bind the result of an if to a name.
It chains consecutive matches that must each succeed. If any of them doesn't match, the chain stops and that value is returned. Because it eliminates nested case stacks, it is widely used in error handling.
Only false and nil are falsey.
There is no else if structure; cond is used instead.
cond always requires a catch-all branch.
case branches based on the structure of the value.
Conditional structures produce values.