On this page:
2.16.1 Multiple Selection
2.16.2 Exercises 9
2.16.2.1 Question 29
2.16.2.2 Question 30
2.16.2.3 Question 31

2.16 Selection

If we look at the mathematical definition of the absolute value function:

\[|x|= \begin{cases} -x, & \text{if $x<0$}\\ x, & \text{otherwise} \end{cases}\]

we notice that it uses a conditional expression in the form of

\[\begin{cases} \mathit{consequent}, & \text{if} \quad\mathit{condition}\\ \mathit{alternative}, & \text{otherwise} \end{cases}\]

that translates to common language as “if condition then consequent, otherwise alternative”.

The evaluation of a conditional expression is done through the evaluation of the condition. If the condition is true, the consequent is evaluated, and if it is false the alternative is evaluated.

Just like mathematics, Julia supports conditional expressions. To that end, it provides two different forms. We will start with the simplest one, called the ternary operator, whose syntax is the following:

condition ? consequent : alternative

The value of a conditional expression is computed in the following way:

  1. The condition is evaluated;

  2. If the previous evaluation is true, the value of the conditional expression is the value of the consequent;

  3. Otherwise (i.e., if the condition turns out to be false), the value of the conditional expression is the value of the alternative.

This behavior can be verified by the following examples:

> 3 > 2 ? 1 : 2

1

> 3 > 4 ? 1 : 2

2

Using a conditional expression, we can now define the absolute value function by translating its mathematical definition to Julia:

abs(x) = x < 0 ? -x : x

The purpose of the conditional expression is to define functions whose behavior depends on one or more conditions. For example, consider the max function that takes two numbers as arguments and returns the largest. To define such a function we only need to test if the first number is bigger than the second. If it is, the function returns the first argument, otherwise it returns the second one. Based on this logic, we can write:

max(x, y) = x > y ? x : y

The other form of Julia’s conditional expressions uses the words if, else, and end, according to the following syntax:

if condition consequent else alternative end

Therefore, the max function can be alternatively written as:

max(x, y) =

  if x > y

    x

  else

    y

  end

Another far more interesting example is the mathematical function \(\operatorname{sgn}\), also known as signum (Latin for “sign”). This function can be interpreted as the dual function of the absolute value function, since \(x=\operatorname{sgn}(x) |x|\). The sgn function is defined as:

The function \(\operatorname{sgn}\) is predefined in Julia with the name sign

\[\operatorname{sgn} x = \begin{cases} -1 & \text{if $x<0$} \\ 0 & \text{if $x = 0$} \\ 1 & \text{otherwise}\end{cases}\]

In natural language, we would say that if \(x\) is negative, the \(\operatorname{sgn} x\) value is \(-1\), otherwise, if \(x\) is \(0\), the value is \(0\), otherwise the value is \(1\). This shows that the above expression uses two conditional expressions stacked in the following way:

\[\operatorname{sgn} x = \begin{cases} -1 & \text{if $x<0$} \\ \begin{cases} 0 & \text{if $x = 0$} \\ 1 & \text{otherwise}\end{cases} & \text{otherwise}\end{cases}\]

To define this function in Julia, two conditional expressions must be used:

sgn(x) = x < 0 ? -1 : (x == 0 ? 0 : 1)

2.16.1 Multiple Selection

When we begin stacking multiple conditional expressions, the program becomes increasingly harder to read. In this case, there is an alternative syntax that might make the function’s definition easier to read. The syntax is as follows:

if condition1

    consequent1

elseif condition2

    consequent2

...

elseif conditionn

    consequentn

else

    alternative

end

The previous form takes as many pairs conditioniconsequenti as needed. Each of these pairs is called a clause. The semantics of a multiple conditional expression is based on the sequential evaluation of the "condition" in each clause until one of them returns true. In that case, the corresponding "consequent" expression is evaluated and the resulting value is returned. If none of the conditions are true, the value of the alternative is returned.

Using the if form makes the sgn function slightly easier to understand:

sgn(x) =

  if x < 0

    -1

  elseif x == 0

    0

  else

    1

  end

2.16.2 Exercises 9
2.16.2.1 Question 29

Define the function sum_largest that, given three numbers as arguments, calculates the sum of the two highest values.

2.16.2.2 Question 30

Define the function max3 that, given three numbers as arguments, returns the maximum value.

2.16.2.3 Question 31

Define the function second_largest that, given three numbers as arguments, returns the second highest number, i.e., the number between the maximum and minimum values.