if - Maple Programming Help

Online Help

All Products    Maple    MapleSim

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


the selection (conditional) statement and operator


Calling Sequence


Conditional Expressions

Nested Conditional Statements

Thread Safety


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



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.


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





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

ifb<athenaelsebend 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&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<cthendabelsedacend 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-statements.




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

See Also







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