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."
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}
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.
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).
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.
Example18.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