Skip to main content

Exploring Calculus with Maple Introductory Calculus

Section 18.1 if/else and elif Statements

‘If’ and ‘else’ statements allow us to test various conditions. The result then changes based on whether the given condition is true or false.
For example, let’s say that someone makes the statement "If it’s sunny tomorrow, then I will go for a bike ride." If it is, in fact, sunny tomorrow, then that person will go for a bike ride. In other words, since the "sunny tomorrow" condition becomes TRUE, then the following implication "go for a bike ride" will happen.
As the statement stands now, we have no idea what this person will do if it is not sunny. Perhaps they will still go for a bike ride, perhaps not. We can define this action with an ‘else’ statement.
In other words, we can define what will happen if it is not sunny. For example: "If it’s sunny tomorrow, then I will go for a bike ride. Otherwise, I will read a book at home."
From this example, we know that the following are true.
  • If it’s sunny tomorrow, then I will go for a bike ride.
  • If it’s not sunny tomorrow, then I read a book at home.

Example 18.1. Conditional Based on Numerical Value.

We will outline this concept with a silly example. We define two variables \(a=2\) and \(b=5\text{.}\) If \(a\lt b\) is true (which it is because obviously \(2\) is less than \(5\)), we print the word "Mario". Otherwise (if it’s false), we print the word "Luigi". Therefore, we expect that "Mario" will be printed. \begin{maplegroup} \begin{mapleinput}

Aside

> a := 2: b := 5:
> if a < b then
  print("Mario")
  else
  print("Luigi")
  end if
\begin{equation*} \displaystyle \text{ "Mario" } \end{equation*}

Example 18.2. Conditional Based on Even/Odd.

Now we consider a more useful example. We define a function \(f(x)\) and check whether substituting \(x=2\) into this function outputs an even number or an odd number.
The type() command is used here to check if the expression has the specified property. Maple recognizes a wide variety of different types, including even and odd.
> f(x) := x^2 + 3*x - 4:
> if type(f(2), even) then
  print(f(2), "even")
  else
  print(f(2), "odd")
  end if
\begin{equation*} \displaystyle 6,\,\text{ "even" } \end{equation*}

Example 18.3. Conditional Based on Finite/Infinite.

In this example, we define a function \(f(x)\) and then use an ‘if’ statement to verify whether or not the limit of \(f(x)\) as \(x\) approaches \(0\) is numeric. In other words, we are checking to see whether or not this limit exists (and is finite).
> f(x) := 1/x:
> L := limit(f(x), x=0):
> if type(L, numeric) then
  print("Limit is defined")
  else
  print("Limit is undefined")
  end if
\begin{equation*} \displaystyle \text{ "Limit is undefined"} \end{equation*}
The elif (else if) command allows us to add more than one condition to our statement. For example, if we want to test whether a particular number is (i) positive, (ii) negative, or (iii) zero, and wish to have different outputs based on these three possibilities, we can do so with a combination of if, else, and elif.

Example 18.4. Multiple Conditional Based on Numerical Value.

In this example, we will use one of these statements to illustrate the first derivative test in calculus. We would like to answer the following question: for a particular value \(x=a\text{,}\) is the function \(g(x)\) (i) increasing (positive derivative), (ii) decreasing (negative derivative), or (iii) neither increasing nor decreasing (zero derivative)?
> g(x) := x^4 - 4*x^3 + 3*x^2:
> a := 0:
> if subs(x = a, diff(g(x), x)) > 0 then
      print("increasing")
  elif subs(x = a, diff(g(x), x)) < 0 then
    print("decreasing")
  else
    print("neither")
  end if
\begin{equation*} \displaystyle \text{ "neither" } \end{equation*}
This implies that \(g'(0)\) is equal to \(0\text{.}\)