if -- branch statement
Introductionif-then-else-end_if allows conditional branching in a
program.
Call(s)
if condition1
then casetrue1
<elif condition2 then
casetrue2>
<elif condition3 then
casetrue3>
<...>
<else casefalse>
end_if _if(condition1, casetrue1,
casefalse)
Parameterscondition1, condition2, ... |
- | Boolean expressions |
casetrue1, casetrue2, ..., casefalse |
- | arbitrary sequences of statements |
Returnsthe result of the last command executed inside the if
statement. NIL is returned
if no command was executed.
Further
DocumentationChapter 17 of the MuPAD Tutorial.
Related
Functions
Detailscondition1 can be evaluated
to TRUE, the branch
casetrue1 is executed and its result is returned.
Otherwise, if condition2 evaluates to TRUE,
the branch casetrue2 is executed and its result is
returned etc. If all of the conditions evaluate to FALSE,
the branch casefalse is executed and its result is
returned.if statement must be reducible to either TRUE
or FALSE. Conditions may be given by equations or inequalities,
combined with the logical operators and, or, not. There is no need to enforce Boolean
evaluation of equations and inequalities via bool. Implicitly, the if
statement enforces ``lazy'' Boolean evaluation via the functions
_lazy_and or
_lazy_or,
respectively. A condition leads to a runtime error if it cannot be
evaluated to TRUE or
FALSE by these
functions. Cf. example 3.end_if may be replaced by the keyword
end.if condition then casetrue else casefalse
end_if is equivalent to the function call _if(condition,
casetrue, casefalse)._if is a function of the system kernel.
Example
1The if statement operates as demonstrated
below:
>> if TRUE then YES else NO end_if, if FALSE then YES else NO end_if
YES, NO
The else branch is optional:
>> if FALSE then YES end_if
NIL
>> if FALSE
then if TRUE
then NO_YES
else NO_NO
end_if
else if FALSE
then YES_NO
else YES_YES
end_if
end_if
YES_YES
Typically, the Boolean conditions are given by
equations, inequalities or Boolean constants produced by system
functions such as isprime:
>> for i from 100 to 600 do
if 105 < i and i^2 <= 17000 and isprime(i) then
print(expr2text(i)." is a prime")
end_if;
if i < 128 then
if isprime(2^i - 1) then
print("2^".expr2text(i)." - 1 is a prime")
end_if
end_if
end_for:
"107 is a prime"
"2^107 - 1 is a prime"
"109 is a prime"
"113 is a prime"
"127 is a prime"
"2^127 - 1 is a prime"
Example
2Instead of using nested if-then-else
statements, the elif statement can make the source code
more readable. However, internally the parser converts such statements
into equivalent if-then-else statements:
>> hold(if FALSE then NO elif TRUE then YES_YES else YES_NO end_if)
if FALSE then
NO
else
if TRUE then
YES_YES
else
YES_NO
end_if
end_if
Example
3If the condition cannot be evaluated to either TRUE or FALSE, then a runtime error is raised.
In the following call, is(x >
0) produces UNKNOWN if no corresponding property was attached to x
via assume:
>> if is(x > 0) then
1
else
2
end_if
Error: Can't evaluate to boolean [if]
Note that Boolean conditions using <, <=, >, >= must not involve symbolic
expressions:
>> if 1 < sqrt(2) then print("1 < sqrt(2)"); end_if
Error: Can't evaluate to boolean [_less]
>> if 1 < float(sqrt(2)) then print("1 < float(sqrt(2))"); end_if
"1 < float(sqrt(2))"
>> if PI < 3.1416 then print("PI < 3.1416"); end_if
Error: Can't evaluate to boolean [_less]
Example
4This example demonstrates the correspondence between the
functional and the imperative use of the if statement:
>> condition := 1 > 0: _if(condition, casetrue, casefalse)
casetrue
>> condition := 1 > 2: _if(condition, casetrue, casefalse)
casefalse
>> delete condition:
end can now be used as
an alternative to end_if.