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.

  1. What does a case expression match its value against?

    Answer

    Patterns

    Each clause of a case is a pattern, and the first pattern that fits runs its body.

  2. How many branches of a case run for one value?

    Answer

    The first matching branch only

    The branches are tried in order, and the first match wins.

  3. What separates two branches of a case?

    Answer

    A semicolon

    Semicolons separate branches, and the last branch ends without one before end.

  4. What does case 2 of 1 -> one; 2 -> two; _ -> other end. return?

    Answer

    two

    The value 2 matches the second branch, so its body two is returned.

  5. What does an if expression test?

    Answer

    Guard sequences

    if has no patterns: each branch is a guard, and the first guard that holds runs.

  6. What does if 2 > 3 -> big; 2 < 3 -> small; true -> equal end. return?

    Answer

    small

    The second guard holds, so small is the result and the later branches are skipped.

  7. What happens when no case branch matches?

    Answer

    A case_clause error is raised

    A failed match raises case_clause with the value that had no branch.

  8. What happens when no if guard holds?

    Answer

    An if_clause error is raised

    An if with no true guard raises if_clause, which is why the catch-all guard is true.

  9. Which pattern is the usual catch-all in a case?

    Answer

    _

    The underscore matches anything and binds nothing, so it accepts every remaining shape.

  10. Which guard is the usual catch-all in an if?

    Answer

    true

    A guard must be an expression, so the final branch uses the literal true rather 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

Banner image