Want to learn?

Control flow

Documentation

In this chapter, you will learn about control flow mechanisms. Before we start you need to know that in Elixir we should use these statements as rarely as possible (when there is no other option). We'll learn about functions soon, and once we do, you'll see that there are alternatives and different approaches when dealing with control flow.

If expression

Documentation

Elixir has two possible syntaxes of the if statement:

  • one-line:

    if 100 == 100, do: "It is true indeed", else: "It is not true"
    
  • multi-line:

    if 100 == 100 do
      "It is true indeed"
    else
      "It is not true"
    end
    

When 100 == 100 logical condition is true, if expression evaluates "It is true indeed" inside do, otherwise "It is not true" from else.

Unless expression

Documentation

Unless is simply the opposite of if. Whenever a logical condition is false unless expression evaluates code inside do, otherwise else. Possible syntaxes:

  • one-line:

    unless 100 == 100, do: "It is not true indeed", else: "It is true indeed"
    
  • multi-line:

    unless 100 == 100 do
      "It is not true"
    else
      "It is true indeed"
    end
    

Cond expression

Documentation

Notice, that inside of if section we have not introduced else if, which is something typical for other programming languages. Why? Because there is no such thing in Elixir.

In Elixir when you want to provide multiple logical paths in control flow, you should use cond:

cond do
  1 + 1 == 1 ->
    "This will never match"
  2 * 2 != 4 ->
    "Nor this"
  true ->
    "This will"
end

Inside of cond do you can provide as many logical conditions as you want. In the above example, we can distinguish three conditions:

  • 1 + 1 == 1,

  • 2 * 2 != 4,

  • true.

Cond is looking for matching logical conditions and finds the first one that evaluates to true. Once it finds such a condition, it executes code followed by the ->arrow. What if there is no match? To make sure that we have alternative code executed when all of the logical conditions are false, you should provide true at the end. Cond evaluates logical conditions from top to bottom, so make sure that true is always the last possible option.

Case expression

Documentation

Case lets you test a value against a set of patterns, executes the code associated with the first pattern that matches, and returns the value of that code. The patterns may include guard clauses (you'll learn about guard classes in the next lesson). In Elixir you'll use case quite often to match tuples against other tuples.

case {1, 2, 3} do
  {4, 5, 6} ->
    "This clause won't match"
  {1, x, 3} ->
    "This clause will match and bind x to 2 in this clause"
  _ ->
    "This clause would match any value"
end

In the following example we are trying to match {1, 2, 3} against three patterns:

  • {4, 5, 6} - no match, since none of these values match each other;

  • {1, x, 3} - match, since 1 and 3 are matching on their corresponding places, and x will bind to 2;

  • _ - this will always match.

Start a project with your dedicated software house

Top

Contact us

* Required fields

Elo Mordo!Elo Mordo!