the selection (conditional) statement and operator - Maple Help

Online Help

All Products    Maple    MapleSim

Home : Support : Online Help : Programming : Flow Control : if

if - the selection (conditional) statement and operator

Calling Sequence

Selection Statement

if <conditional expression> then <statement sequence>

| elif <conditional expression> then <statement sequence> |

| else <statement sequence> |

end if

(Note: Phrases located between | | are optional.)


if operator

`if`(conditional expression, true expression, false expression)



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.


For more information about displaying information, see printlevel and print. For a definition, see if .

Thread Safety


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


For more information on thread safety, see index/threadsafe.


Simple Case 1





Since a (3) is not less than b (5), the b branch is followed.

ifb<athenaelsebend if



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.




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.


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&apos;FAIL&apos;then3else5end if



Nested Conditional Statements




In the following equation there are two nested conditions. Since a<b is true and a<c is false, d is assigned the calculated value a*c.

ifa<bthenifa<cthend:=abelsed:=acend ifend if



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.

ifa&equals;1thenprintfirstelifa&equals;2thenprintsecondelifa&equals;3thenprintthirdend if



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.




ifa&equals;1thenprintfirstelifa&equals;2thenprintsecondelifa&equals;3thenprintthirdend if

See Also

boolean, print, printlevel, statement

Download Help Document

Was this information helpful?

Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam