Previous Page Next Page Contents

if -- branch statement

Introduction

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

Parameters

condition1, condition2, ... - Boolean expressions
casetrue1, casetrue2, ..., casefalse - arbitrary sequences of statements

Returns

the result of the last command executed inside the if statement. NIL is returned if no command was executed.

Further Documentation

Chapter 17 of the MuPAD Tutorial.

Related Functions

case, piecewise

Details

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
                                  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 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
                                NO
                              else
                                if TRUE then
                                  YES_YES
                                else
                                  YES_NO
                                end_if
                              end_if

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 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 4

This 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:

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000