Skip to main content \(\newcommand{\longdivision}[2]{#1\big)\!\!\overline{\;#2}}
\newcommand{\mlongdivision}[2]{\longdivision{#1}{#2}}
\renewcommand{\emptyset}{\{\}}
\newcommand{\blanksp}{\underline{\hspace{.25in}}}
\newcommand{\set}[1]{\left\{#1\right\}}
\newcommand{\cspace}{-}
\newcommand{\Ta}{\mathtt{a}}
\newcommand{\Tb}{\mathtt{b}}
\newcommand{\Tc}{\mathtt{c}}
\newcommand{\Td}{\mathtt{d}}
\newcommand{\Te}{\mathtt{e}}
\newcommand{\Tf}{\mathtt{f}}
\newcommand{\Tg}{\mathtt{g}}
\newcommand{\Th}{\mathtt{h}}
\newcommand{\Ti}{\mathtt{i}}
\newcommand{\Tj}{\mathtt{j}}
\newcommand{\Tk}{\mathtt{k}}
\newcommand{\Tl}{\mathtt{l}}
\newcommand{\Tm}{\mathtt{m}}
\newcommand{\Tn}{\mathtt{n}}
\newcommand{\To}{\mathtt{o}}
\newcommand{\Tp}{\mathtt{p}}
\newcommand{\Tq}{\mathtt{q}}
\newcommand{\Tr}{\mathtt{r}}
\newcommand{\Ts}{\mathtt{s}}
\newcommand{\Tt}{\mathtt{t}}
\newcommand{\Tu}{\mathtt{u}}
\newcommand{\Tv}{\mathtt{v}}
\newcommand{\Tw}{\mathtt{w}}
\newcommand{\Tx}{\mathtt{x}}
\newcommand{\Ty}{\mathtt{y}}
\newcommand{\Tz}{\mathtt{z}}
\newcommand{\So}{\Tf}
\newcommand{\Sno}{\Tg}
\newcommand{\Si}{\Th}
\newcommand{\Sni}{\Tj}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\W}{\mathbb{W}}
\newcommand{\ZZ}{\Z}
\newcommand{\PP}{\mathbb{P}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\RR}{\R}
\newcommand{\F}{\mathbb{F}}
\newcommand{\A}{\mathbb{A}}
\newcommand{\abs}[1]{|#1|}
\newcommand{\fmod}{\bmod}
\newcommand{\fdiv}{\,\mathrm{div}\,}
\newcommand{\lcm}{\mathrm{lcm}}
\newcommand{\id}{\mathrm{id}}
\newcommand{\nr}[1]{\##1}
\newcommand{\gexp}[3]{#1^{#2 #3}}
\newcommand{\gexpp}[3]{\displaystyle\left(#1\right)^{#2 #3}}
\newcommand{\glog}[3]{\log_{#1}^{#3}#2}
\newcommand{\sol}[1]{{\color{blue}\textit{#1}}}
\newcommand{\gro}[1]{{\color{gray}#1}}
\newcommand{\todo}[1]{{\color{purple}TO DO: #1}}
\newcommand{\fixme}[1]{{\color{red}FIX ME: #1}}
\newcommand{\checkme}[1]{{\color{green}CHECK ME: #1}}
\newcommand{\degre}{^\circ}
\newcommand{\vect}[1]{\overrightarrow{#1}}
\newcommand{\nix}{}
\newcommand{\cox}[1]{\fcolorbox[HTML]{000000}{#1}{\phantom{M}}}
\newcommand{\tox}[1]{\texttt{\##1} \amp \cox{#1}}
\newcommand{\ttx}[1]{\texttt{\##1}}
\newcommand{\mox}[1]{\mathtt{\##1}}
\newcommand{\xx}{\mathtt{\#}}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
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_then by Matt Farmer and Stephen Steward
Algorithm 2.12 . Maximum of two integers.
Input:
two integers
\(a\) and
\(b\)
Output:
the maximum of
\(a\) and
\(b\)
if \(a\ge b\) then return \(a\)
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\)
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 .
return \(b\) — The algorithm returns the value of the variable
\(b\) which is
\(5\text{.}\)
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\) .
Input : \(a=3\) and \(b=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.
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\) .
Input : \(a=11\) and \(b=11\)
Because
\(11\ge 11\) is true. Thus we follow the instruction after
then and return 3.
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.
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{.}\)
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{.}\)
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.
As
\(0\) is not negative we have
\(\abs{0}=0\)
As
\(8\) is not negative we have
\(\abs{8}=8\text{.}\)
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.
Algorithm 2.19 . Absolute value.
Input:
Output:
the absolute value of
\(b\)
if \(b\ge 0\) then return \(b\)
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\)
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{.}\)
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\)
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.
return \(-b\text{:}\) We compute
\(-b\) which is
\(-(-6)=6\) and return
\(6\text{.}\)
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.