IF INDEX

IF _ _ _ _ _ _ _ _ _ _ _ _ command

The if command is a conditional statement that executes a statement if a condition is true, and optionally another statement if it is not.

syntax:

if<condition> then <statement> _ _ _ option(else <statement>)

<condition> must be a logical or comparison operator that evaluates to a boolean value. <statement> must be a single REDUCE statement or a group (<<...>>) or block (begin...end) statement.

examples:


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


  D + F 


x := 9; 

  X := 9 


if numberp x and x<20 then y := sqrt(x) else write "illegal";
			 


  3  


clear x; 

if numberp x and x<20 then y := sqrt(x) else write "illegal";
			 


  illegal 


x := 12; 

  X := 12 


a := if x < 5 then 100 else 150;
			 


  A := 150 


b := u**(if x < 10 then 2);
			 

  B := 1 


bb := u**(if x > 10 then 2);
			 

         2
  BB := U

An if statement may be used inside an assignment statemen t and sets its value depending on the conditions, or used anywhere else an expression would be valid, as shown in the last example. If there is no else clause, the value is 0 if a number is expected, and nothing otherwise.

The else clause may be left out if no action is to be taken if the condition is false.

The condition may be a compound conditional statement using and or or. If a non-conditional statement, such as a constant , is used by accident, it is assumed to have value true.

Be sure to use group or block statements after then or else.

The if operator is right associative. The following constructions are examples:

_ _ _ (1)

syntax:

if<condition> then if <condition> the n <action> else <action>

which is equivalent to

syntax:

if<condition> then (if <condition> then <action> else <action>);

_ _ _ (2)

syntax:

if<condition> then <action> else if <condition> then <action> else <action>

which is equivalent to

syntax:

if<condition> then <action> else

(if <condition> then <action> else <action>).