if - Maple Programming 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.)

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 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.
 • For a function or operator form of conditional selection, see the ifelse function.

Conditional Expressions

 • 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.

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

 > $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)

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}$ (3)

Nested Conditional Statements

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

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}}$ (6)
 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$
 ${a}{≔}{4}$ (7)
 > $\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}$