Video outline

As you remember from the previous video on the control flow statements, these statements are mainly used to determine which blocks of the code will be executed and which will not be shown. Some examples of the control flow statements include: if, elsif, else, unless, case and when. Today we will mainly focus on the last two statements, case and when.

What are case and when statements?

So what are the case and when statements. A case statement is another control flow statement that is used to compare values in the same variable. It is similar to a series of if statements on the same expression. It is always used in a combination with a when statement, so these two statements always go together.

A when statement can be used to check the values of a variable and only execute code when that variable contains the intended value.

A great example on where these statements can come in handy would be the application of an interest rate, as it may change depending on the financial year. You can then create a case statement based on the year and assign a different rate, based on the fiscal year you are in. We will have a deeper look at this example later in this video.

To start your case statement, you can just type a word “case” and press the tab key. You should immediately see a predetermined set up that is required to build a case statement.

As you can see here, the case statement has an opening tag and an end tag. The value that needs to be checked will usually be stored in a variable. So you can replace the word value with the variable name that needs to be checked.

You can then check that variable for its content using the when statements. The when statements are actually the conditions that you will be checking. If these conditions are met, the code below will be executed and you will see that particular content in the template. Please note that once a condition is true, the system stops reading the code further and just returns the result.

At the end there is an else statement that can be used in a combination with a when statement to cover all other possibilities. In other words if none of the when conditions are true, the system will return the value in the else clause.

So as we just saw, a case statement can consist of three parts: opening and closing tags, when statements and an else statement at the end.

Now let’s have a look at the example on how interest rates can be defined depending on the fiscal year.

Example

Let’s assume that we have a variable fiscal_year that is equal to 2021.

Then we type a word case followed by a tab to create the set up for the case statement. Inside the opening tag of the case statement we type the name fiscal_year which is the name of the variable that we have just assigned. In this case, the fiscal year will contain a value of 2021.

After this we will use when statements to check multiple conditions. In this example, we have two conditions to check:

  • the first one is when 2020; and
  • the second one is when 2021.

So this will read the same as when the fiscal year equals to 2020 and when the fiscal year equals to 2021. Now we need to add some content to our conditions.

In the first case, when the fiscal year is 2020 our interest_rate will be equal to 0.23, when the fiscal year is 2021 our interest_rate will be equal to 0.27.

As you can see from this example, in the when statements we only have values that we are checking. This is because in the case and when statements we are only checking if a value is equal to the value that is stored in the variable, which is defined in the opening tag of the case statement.

You have probably noticed that there is also an else statement. This is added to cover all other possibilities. In our case all other financial years. Meaning that in case the fiscal year is before 2020 or after 2021, the interest_rate variable will contain a value of 0.19.

{% case fiscal_year %}
{% when 2020 %}
  {% assign interest_rate = 0.23 %}
{% when 2021 %}
  {% assign interest_rate = 0.27 %}
{% else %}
  {% assign interest_rate = 0.19 %}
{% endcase %}

{{interest_rate}}

Now that we have set up our case when statement, we have an interest_rate variable that depends on the fiscal year we are working in. In our example, we are in 2021 fiscal year, thus, we will enter the third when statement and the value of our interest_rate variable will be equal to 0.27 or 27%.

Conclusion

  • Case and when statements are used to check, compare value in the same variable.
  • Case statement is always used in a combination with a when statement.
  • Case statement can contain three parts.
  • Case and when statements can be used to determine a value on a specified condition.