the selection (conditional) statement and operator - Maple Help

if - the selection (conditional) statement and operator

 Calling Sequence Selection Statement if  then | elif  then  | | else  | end if (Note: Phrases located between | | are optional.)   if operator if(conditional expression, true expression, false expression)

Description

 • 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 short form avoids the requirement for multiple closing end if delimiters.
 • 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.
 • The statement sequence following else is executed if all of the conditional expressions evaluate to false or FAIL.
 • The operator form of if requires three arguments and returns the evaluation of the second or third, depending on the truth value of the first.  The first argument is evaluated to a boolean and, if true, the second argument is evaluated and returned.  If the first argument evaluates to false or FAIL, the third argument is evaluated and returned.
 When using the operator form, the name of this function must be enclosed in back quotes (left single quotes) because it is a Maple reserved word.
 • This statement has special evaluation rules in that the arguments are not evaluated if they are not required.
 • Note about Nested Conditional Statements
 Statements in a Maple session are recognized in levels, determined by the nesting of conditional or repetition statements and the nesting of procedures. In particular, the top (interactive) level is level $0$; statements within conditional 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 conditional 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 conditional statements.
 An alternative method of displaying the result of a statement that is nested inside loops or conditional statements is to use the statement in conjunction with the print command.

 • The if statement and operator is thread-safe as of Maple 15.

Examples

Simple Case 1

 > $a:=3;$$b:=5$
 ${a}{:=}{3}$
 ${b}{:=}{5}$ (1)
 Since a (3) is not less than b (5), the b branch is followed.
 > $\mathbf{if}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}b
 ${5}$ (2)

Simple Case 2 Using if operator

 The conditional statement if operator can be used inside an equation. Since b (3) is not less than a (5), the result of the inner if command will be the false expression, b, which is used as part of the calculation.
 > $5\left(\mathrm{π}+\mathrm{if}\left(b
 ${5}{}{\mathrm{π}}{+}{25}$ (3)

Using  if operator with NULL

 Since a is less than b, x is assigned NULL. Unlike every other function call, which removes NULLs from the calling sequence, NULL can be used with the if operator.
 > $x:=\mathrm{if}\left(a

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.
 > $\mathbf{if}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}'\mathrm{FAIL}'\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}3\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{else}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}5\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end if}$
 ${5}$ (4)

Nested Conditional Statements

 > $c:=1$
 ${c}{:=}{1}$ (5)
 In the following equation there are two nested conditions. Since a
 > $\mathbf{if}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}a
 ${3}$ (6)

Using elif in a Conditional Statement

 elif can be repeated any number of times in a statement. In this example the result of the conditional statement is used in conjunction with the print command. Since a is true, the output is third.
 > $\mathbf{if}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}a=1\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{print}\left(\mathrm{first}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{elif}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}a=2\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{print}\left(\mathrm{second}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{elif}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}a=3\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{print}\left(\mathrm{third}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end if}$
 ${\mathrm{third}}$ (7)
 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-statments.
 > $a:=4$
 ${a}{:=}{4}$ (8)
 > $\mathbf{if}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}a=1\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{print}\left(\mathrm{first}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{elif}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}a=2\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{print}\left(\mathrm{second}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{elif}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}a=3\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{print}\left(\mathrm{third}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end if}$