the selection (conditional) statement and operator
Nested Selection Statements
Selection Statements within Expressions
if <conditional expression> then <statement sequence>
| elif <conditional expression> then <statement sequence> |
| else <statement sequence> |
( Note: Phrases located between | | are optional.)
The selection statement causes the statement sequence in the selected branch (if any) to execute.
The construct elif conditional expression then statement sequence can be repeated any number of times. The keyword elif stands for else if.
The statement sequence following else is executed if all of the preceding conditional expressions evaluate to false or FAIL.
This statement has special evaluation rules in that the arguments are not evaluated if they are not required.
The end if that terminates a selection statement can be shortened to just end, or the ALGOL-like fi, if desired.
A conditional expression is any Boolean expression formed by using the relational operators ( <, <=, >, >=, =, <> ), the logical operators (and, or, not), and the logical names (true, false, FAIL).
When a conditional expression is evaluated in this context, it must evaluate to true or false or FAIL; otherwise, an error occurs.
Statements in a Maple session are recognized in levels, determined by the nesting of selection or repetition statements and the nesting of procedures. In particular, the top (interactive) level is level 0; statements within selection and repetition statements are level 1, or level 2 if doubly nested, etc.
The setting of printlevel causes the display of the results of all statements executed up to the level indicated by printlevel. By default, printlevel is initially set to 1. As a result, if statements are located inside nested loops or nested selection statements, it may be necessary to set printlevel to a higher value in order to see the results of all the statements located in the loops or selection statements.
An alternative method of displaying the result of a statement that is nested inside repetition or selection statements is to use the statement in conjunction with the print command.
For more information about displaying information, see printlevel and print. For a definition, see if.
The ifelse function allows a selection to be computed as, or within, an expression. However, this is limited to two branches, and each branch can consist of only an expression.
An actual selection statement can also be used as an expression or subexpression. Unlike the aforementioned ifelse function, an embedded selection statement can have more than two branches, and each branch can consist of multiple statements.
The value of such an embedded selection statement is the value of the last expression computed within the branch that was executed.
Note: Selection statements embedded within expressions are currently not supported in 2-D input in the Standard interface.
The if statement and operator is thread-safe as of Maple 15.
For more information on thread safety, see index/threadsafe.
a := 3; b := 5;
Since a (3) is not greater than b (5), the b branch is followed.
if (a > b) then a else b end if;
Testing Against FAIL
FAIL is used by the Boolean logic to mean an unknown or undetermined value. A FAIL has the same effect as false.
if 'FAIL' then 3 else 5 end if;
c := 2;
In the following equation there are two nested selections. Since a<b is true and a<c is false, d is assigned the calculated value a*c. We've set printlevel to 2 to see this.
if a < b then
if a < c then d := a*b else d := a*c end if
Using elif in a Selection Statement
elif can be repeated any number of times in a statement. In this example the result of the selection statement is used in conjunction with the print command. Since a is true, the output is third.
if a = 1 then print(first)
elif a = 2 then print(second)
elif a = 3 then print(third)
When a is assigned a value of 4, no conditions evaluate to true, and there is no else case, so the if-statement completes without evaluating any sub-statements.
a := 4;
Using a Selection Statement within an Expression
a := 3;
s := String(a, if a = 1 then "st"
elif a = 2 then "nd"
elif a = 3 then "rd"
else "th" end if);
Download Help Document
What kind of issue would you like to report? (Optional)