Elixir basics · Lesson 06 (open contents)
01 · Let the code speak02 · Meet six kinds of values03 · Put values together04 · Build a new list05 · Make the shapes match06 · Choose with case07 · Parse a number safely08 · Unpack &1 and the pipe09 · Put code in a project10 · Ask precise true-or-false questions11 · Read and update nested data12 · Count visible text and bytes13 · Let function clauses choose14 · Solve a list twice15 · Choose the smallest clear control form16 · Give domain data a shape17 · Put one tested module in Mix18 · Make success and failure predictable19 · Read and write one real file20 · Take only the data you need21 · Give different data one shared action22 · Write a callback contract23 · Write down the data contract24 · Test behavior and boundaries25 · Turn a project into a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ELIXIR · SCRATCH · LESSON 0625 minutes

Choose with case

Match one value against branches, then add conditions with guards.

01 · Start with the whole example

Choose a message for a score

Change score to 95 and 40, and run the code each time.

Elixir
# score is the value we will check
score = 76

# case looks for the first matching branch from top to bottom
message =
  case score do
    value when value >= 90 -> "Great work"
    value when value >= 60 -> "Passed"
    _value -> "Try again"
  end

# Look at the chosen result
message
Check the result first
  • 76 produces "Passed".
  • 95 produces "Great work".
  • 40 produces "Try again".
02 · Take the code apart

Read from the first line down

  1. case score do says that the next branches will check score.
  2. Branches are tried from top to bottom, so >= 90 must come before >= 60.
  3. _value catches any remaining value. Its leading underscore reminds us that we will not use it again.
03 · Meet the new symbols

Symbols are not secret signs

->

When the condition on the left matches, run the expression on the right.

when

Add one more condition after a pattern.

_

A final fallback branch that catches values not handled above.

case ... do ... end

do opens the branches. end closes the whole case.

04 · Ideas inside the code

Match each name to its meaning

01

branch

A piece of code chosen for a particular kind of input.

02

case

Match one value against several patterns and run the first matching branch.

03

guard

An extra condition after when, such as when score >= 60.

05 · Make the idea clear

Why these forms are useful

Programs often need to act differently in different situations. case gets one value, then tries to match it against several patterns.

Each branch says which input it accepts and what it returns. The first matching branch runs. The later branches are skipped.

A guard is a small condition after a pattern. Guards allow only a safe, clearly defined group of checks.

06 · Change it yourself

Close the answer and try

Choose an action for a weather atom: take an umbrella for :rain, wear a hat for :sunny, and look at the sky for anything else.

Practice starting point
# Try other atoms too
weather = :rain

case weather do
  ____ -> "Take an umbrella"
  ____ -> "Wear a hat"
  ____ -> "Look at the sky"
end

Target result: :rain produces "Take an umbrella", and :sunny produces "Wear a hat".

Stuck? Read one hint

Use fixed atoms in the first two places. Use an underscore name to catch every other value.

After you run it, see one answer
One answer
# weather is the value to match
weather = :rain

case weather do
  :rain -> "Take an umbrella"
  :sunny -> "Wear a hat"
  _other -> "Look at the sky"
end
Think about it: Why must `value >= 90` come before `value >= 60`?

case picks the first matching branch. Since 95 is also above 60, the opposite order would send 95 into the “Passed” branch too soon.

Take with you

Remember these three lines

  1. 1

    case is good at combining pattern matching with a choice.

  2. 2

    The order of branches can change the result.

  3. 3

    A fallback branch explains what happens to other inputs.

Lesson completeRun the code once, then mark this lesson as done.