Skip to main content
Logo image

Section 2.3 The Conditional if-then

A conditional tells us which instruction to follow based on 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.3.1 we introduce the if-then instruction and give the first examples.
Figure 2.3.1. Algorithms -- if-then by Matt Farmer and Stephen Steward
Consider the algorithm:
We follow the instructions in Algorithm 2.3.2 for different combinations of input values.

Example 2.3.3. Algorithm 2.3.2 with input \(a=2\) and \(b=5\).

We follow Algorithm 2.3.2 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.3.2 is 5.

Example 2.3.4. Algorithm 2.3.2 with input \(a=3\) and \(b=1\).

We follow Algorithm 2.3.2 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{.}\) Thus \(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.3.2 is 3.

Example 2.3.5. Algorithm 2.3.2 with input \(a=11\) and \(b=11\).

We follow Algorithm 2.3.2 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.3.2 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.3.6.

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.3.7. 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. If \(b\) is negative, its distance from \(0\) is still positive. In words, finding the absolute value of \(b\) means removing the negative sign. This idea is easy to describe in words, but harder to write in mathematical notation. Using mathematical operations, we say that if \(b\) is negative, the absolute value of \(b\) is \(-b\text{,}\) which is positive.

Example 2.3.8. 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 write an algorithm that returns the absolute value of an integer.
We follow the instructions in Algorithm 2.3.9 for different combinations of input values.

Example 2.3.10. Algorithm 2.3.9 with input \(b=7\).

We follow the instructions in   2.3.9. for the input \(b=7\text{.}\)
Input: \(b=7\)
  1. if \(b\ge 0\) then return \(b\) : Because \(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.3.9 is \(7\text{.}\)

Example 2.3.11. Algorithm 2.3.9 with input \(b=-6\).

We follow the instructions in Algorithm 2.3.9 for the input \(b=-6\text{.}\)
Input: \(b=-6\)
  1. if \(b\ge 0\) then return \(b\text{:}\) Because \(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.3.9 is \(6\text{.}\)