Skip to main content
Logo image

Section 2.3 The Conditional if_then

Conditionals are used to specify which instruction should be followed depending whether a statement is true or false.
The conditional has the two parts if and then. If the statement after if is true, the instruction that follows then is executed. If the statement after if is false, we do not execute the instruction that follows then, and instead we continue with the next instruction.
In the video in Figure 2.11 we introduce the if_then instruction and give first examples.
Figure 2.11. Algorithms -- if_thenby Matt Farmer and Stephen Steward
Consider the algorithm:
We follow the instructions in Algorithm 2.12 for different combinations of input values.

Example 2.13. Algorithm 2.12 with input \(a=2\) and \(b=5\).

We follow Algorithm 2.12 for the input values \(a=2\) and \(b=5\text{.}\) For each (numbered) line in the sequence of instructions we describe what we do.
Input: \(a=2\) and \(b=5\)
  1. if \(a\ge b\) then : We check whether the value of \(a\) is greater than the value of \(b\text{.}\) If this is true we follow the instruction after then, otherwise we continue wit the next step. The value of \(a\) is 2 and the value of \(b\) is 5. Because \(2\ge 5\) is false, we do not follow the instruction after then and continue with step 2.
  2. return \(b\) — The algorithm returns the value of the variable \(b\) which is \(5\text{.}\)
Output: \(5\)
So when the input is \(a=2\) and \(b=5\) the output of Algorithm 2.12 is 5.

Example 2.14. Algorithm 2.12 with input \(a=3\) and \(b=1\).

We follow Algorithm 2.12 for the input \(a=3\) and \(b=1\text{.}\)
Input: \(a=3\) and \(b=1\)
  1. if \(a\ge b\) then return \(a\) : We have \(a=3\) and \(b=1\text{.}\) This \(a\ge b\) is true. We follow the instruction after then and return 3.
Output: \(3\)
So when the input is \(a=3\) and \(b=1\) the output of Algorithm 2.12 is 3.

Example 2.15. Algorithm 2.12 with input \(a=11\) and \(b=11\).

We follow Algorithm 2.12 for the input \(a=11\) and \(b=11\text{.}\)
Input: \(a=11\) and \(b=11\)
  1. Because \(11\ge 11\) is true. Thus we follow the instruction after then and return 3.
Output: \(11\)
So when the input is \(a=11\) and \(b=11\) the output of Algorithm 2.12 is 11.

Subsection Absolute Value

Our next goal is the formulation of an algorithm that returns the absolute value of an integer. We start with a definition of the absolute value of an integer.

Definition 2.16.

The absolute value of an integer \(b\) is its distance from zero. We denote the absolute value of an integer \(a\) by \(|a|\text{.}\)
A distance between two integers is always a non-negative integer. So the absolute value of an integer is a non-negative integer.

Example 2.17. Absolute values as distance from \(0\).

We give examples of absolute values.
  1. The absolute value of \(2\) is \(2\text{,}\) as the distance of \(2\) from \(0\) (on the number line) is \(2\text{.}\) We write \(|2|=2\text{.}\)
  2. The absolute value of \(-2\) is \(2\text{,}\) as the distance of \(-2\) from \(0\) (on the number line) is \(2\text{.}\) We write \(|-2|=2\text{.}\)
  3. The absolute value of \(0\) is \(0\text{,}\) as the distance of \(0\) from \(0\) (on the number line) is \(0\text{.}\) We write \(|0|=0\text{.}\)
If an integer \(b\) is positive, then its absolute value is the integer \(b\) itself. When \(b\) is negative, its distance from \(0\) still is positive. We can casually describe finding the absolute value of \(b\) as removing the negative sign. This is easy to describe in words, but not easy in mathematical notation. To write this using mathematical operations, we say that if \(b\) is negative, the absolute value of \(b\) is \(-b\text{,}\) which is positive.

Example 2.18. Absolute values algebraically.

We compute the absolute values of some integers.
  1. As \(0\) is not negative we have \(\abs{0}=0\)
  2. As \(8\) is not negative we have \(\abs{8}=8\text{.}\)
  3. As \(-10\) is less than zero the absolute value of \(-10\) is the negative of \(-10\text{,}\) that is, \(\abs{-10} =-(-10)=10\text{.}\)
We are now ready to formulate an algorithms that returns the absolute value of an integer.
We follow the instructions in Algorithm 2.19 for different combinations of input values.

Example 2.20. Algorithm 2.19 with input \(b=7\).

We follow the instructions in   2.19. for the input \(b=7\text{.}\)
Input: \(b=7\)
  1. if \(b\ge 0\) then return \(b\) : As \(b=7\) the statement \(b\ge 0\) is true. Hence we follow the instruction after then and return the value of \(b\) which is \(7\text{.}\)
Output: \(7\)
So when the input is \(b=7\text{,}\) the output of Algorithm 2.19 is \(7\text{.}\)

Example 2.21. Algorithm 2.19 with input \(b=-6\).

We follow the instructions in Algorithm 2.19 for the input \(b=-6\text{.}\)
Input: \(b=-6\)
  1. if \(b\ge 0\) then return \(b\text{:}\) As \(b=-6\) the statement \(-6\ge 0\) is false. Hence we continue with the next instruction.
  2. return \(-b\text{:}\) We compute \(-b\) which is \(-(-6)=6\) and return \(6\text{.}\)
Output: \(6\)
So when the input is \(b=-6\) the output of Algorithm 2.19 is \(6\text{.}\)
In Checkpoint 2.22 determine the values returned for by the algorithm for the given input values and determine what its output is.

Checkpoint 2.22. Find the output values and determine the output.

Consider the algorithm:
Algorithm
Input: Two integers \(a\) and \(b\text{.}\)
(1) if \(a \lt b\) then return \(a\)
(2) return \(b\)
What does the algorithm return when the input is \(a=-86\) and \(b=-60\) ?
What does the algorithm return when the input is \(a=-55\) and \(b=64\) ?
What does the algorithm return when the input is \(a=-21\) and \(b=-75\) ?
What is the Output of the algorithm ?
  • The sum of \(a\) and \(b\)
  • The minimum of \(a\) and \(b\)
  • The greatest common divisor of \(a\) and \(b\)
  • The absolute value of \(a\)
  • The maximum of \(a\) and \(b\)