This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

if, then, elif, else, end_if, _if

If-statement (conditional branch in a program)

For the if statement in MATLAB®, see if.

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


if condition1
then casetrue1
   elif condition2 then casetrue2
   elif condition3 then casetrue3
   else casefalse

_if(condition1, casetrue1, casefalse)


if-then-else-end_if allows conditional branching in a program.

If the Boolean expression condition1 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.

All conditions that are evaluated during the execution of the 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.

The keyword end_if may be replaced by the keyword end.

The statement if condition then casetrue else casefalse end_if is equivalent to the function call _if(condition, casetrue, casefalse).


Example 1

The if statement operates as demonstrated below:

if TRUE then YES else NO end_if,
if FALSE then YES else NO end_if

The else branch is optional:

if FALSE then YES end_if
  then if TRUE 
         then NO_YES
         else NO_NO
  else if FALSE
         then YES_NO
         else 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")
  if i < 128 then
     if isprime(2^i - 1) then
        print("2^".expr2text(i)." - 1 is a prime")

Example 2

Instead 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
  if TRUE then

Example 3

If 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 properties was attached to x via assume:

if is(x > 0) then
Error: Cannot evaluate to Boolean. [if]

Note that Boolean conditions using <, <=, >, >= may fail if they involve symbolic expressions:

if 1 < sqrt(2) then print("1 < sqrt(2)"); end_if

if 10812186006/7645370045 < sqrt(2)
  then print("10812186006/7645370045 < sqrt(2)");

if is(10812186006/7645370045 < sqrt(2)) = TRUE
  then print("10812186006/7645370045 < sqrt(2)");

Example 4

This example demonstrates the correspondence between the functional and the imperative use of the if statement:

condition := 1 > 0: _if(condition, casetrue, casefalse)

condition := 1 > 2: _if(condition, casetrue, casefalse)

delete condition:


condition1, condition2, …

Boolean expressions

casetrue1, casetrue2, casefalse, …

Arbitrary sequences of statements

Return Values

Result of the last command executed inside the if statement. The empty sequence, null() is returned if no command was executed.

See Also

MuPAD Functions

More About

Was this topic helpful?