REDUCE

5.3 Conditional Statements

The conditional statement has the following syntax:

\(\langle \)conditional statement\(\rangle \) \(\longrightarrow \) if \(\langle \)boolean expression\(\rangle \)
then \(\langle \)statement\(\rangle \)
[else \(\langle \)statement\(\rangle \)]

The boolean expression is evaluated. If this is true, the first \(\langle \)statement\(\rangle \) is executed. If it is false, the second is.

Examples:

        if x=5 then a:=b+c else d:=e+f

        if x=5 and numberp y
           then <<ff:=q1; a:=b+c>>
           else <<ff:=q2; d:=e+f>>

Note the use of the group statement.

Conditional statements associate to the right; i.e.,

        IF <a> THEN <b> ELSE IF <c> THEN <d> ELSE <e>

is equivalent to:

        IF <a> THEN <b> ELSE (IF <c> THEN <d> ELSE <e>)

In addition, the construction

        IF <a> THEN IF <b> THEN <c> ELSE <d>

parses as

        IF <a> THEN (IF <b> THEN <c> ELSE <d>).

If the value of the conditional statement is of primary interest, it is often called a conditional expression instead. Its value is the value of whichever statement was executed. (If the executed statement has no value, the conditional expression has no value or the value 0, depending on how it is used.)

Examples:

        a:=if x<5 then 123 else 456;
        b:=u + v^(if numberp z then 10*z  else 1) + w;

If the value is of no concern, the else clause may be omitted if no action is required in the false case.

        if x=5 then a:=b+c;

Note: As explained in Section 3.3, if a scalar or numerical expression is used in place of the boolean expression – for example, a variable is written there – the true alternative is followed unless the expression has the value 0.


Hosted by Download REDUCE Powered by MathJax