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.

Check our latest product - it's based on our experience of managing over 50-people strong company. The tool we're missing as a small company and not an enterprise.

humadroid.io is an employee and performance management software. It's an unique tool allowing everyone to be in the loop - by having up to date info about co-workers, time-off, benefits, assets, helping with one-on-ones, being a go-to place for company-wide announcements.

Check out humadroid.io
Top

Contact us

* Required fields

The controller of your personal data provided via this contact form is Prograils sp. z o.o., with a registered seat at Sczanieckiej 9A/10, 60-215 PoznaƄ. Your personal data will be processed in order to respond to your inquiries and for our marketing purposes (e.g. when you ask us for our post-development, maintenance or ad hoc engagements for your app). You have the rights to: access your personal data, rectify or erase your personal data, restrict the processing of your personal data, data portability and to object to the processing of your personal data. Learn more.

Notice

We do not track you online. We use only session cookies and anonymous identifiers for the purposes specified in the cookie policy. No third-party trackers.

I understand
Elo Mordo!Elo Mordo!