Conditions: case and if
case of
The case expression matches its value against patterns, one arrow at a time. The first pattern that matches runs its body.
Patterns are the same ones you used in lesson 5, so literals, variables, and tuples all work here.
erl
1> case 2 of
2> 1 -> one;
3> 2 -> two;
4> _ -> other
5> end.
two
Arrows and bodies
Each branch is a pattern, the arrow, and a body expression. Semicolons separate branches, and the last branch has no semicolon.
The body may be any expression, including a begin block of several lines when the work needs more room.
case Status of
ok -> handle(Data);
{error, Why} -> log(Why)
end.
if
The if expression tests guard sequences instead of patterns. It fits simple value tests, and case fits shape tests.
Both select the first branch that holds, so order matters when branches overlap.
erl
1> if
2> 2 > 3 -> big;
3> 2 < 3 -> small;
4> true -> equal
5> end.
small
The true branch
Every case and if needs a catch-all. When no branch matches, the runtime raises a case_clause or if_clause error with the failing value attached.
The catch-all is the pattern _ or the guard true. Make it the last line of every conditional you write.
case X of
special -> ok;
_ -> io:format("unexpected: ~p~n", [X])
end.
Next: Recursion replaces loops
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a
caseexpression match its value against?Answer
Patterns
Each clause of a
caseis a pattern, and the first pattern that fits runs its body. -
How many branches of a
caserun for one value?Answer
The first matching branch only
The branches are tried in order, and the first match wins.
-
What separates two branches of a
case?Answer
A semicolon
Semicolons separate branches, and the last branch ends without one before
end. -
What does
case 2 of 1 -> one; 2 -> two; _ -> other end.return?Answer
twoThe value 2 matches the second branch, so its body
twois returned. -
What does an
ifexpression test?Answer
Guard sequences
ifhas no patterns: each branch is a guard, and the first guard that holds runs. -
What does
if 2 > 3 -> big; 2 < 3 -> small; true -> equal end.return?Answer
smallThe second guard holds, so
smallis the result and the later branches are skipped. -
What happens when no
casebranch matches?Answer
A
case_clauseerror is raisedA failed match raises
case_clausewith the value that had no branch. -
What happens when no
ifguard holds?Answer
An
if_clauseerror is raisedAn
ifwith no true guard raisesif_clause, which is why the catch-all guard istrue. -
Which pattern is the usual catch-all in a
case?Answer
_The underscore matches anything and binds nothing, so it accepts every remaining shape.
-
Which guard is the usual catch-all in an
if?Answer
trueA guard must be an expression, so the final branch uses the literal
truerather than a pattern.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev