Example: Two Conditional Actions. This program takes a value x from the stack and calculates (sin x)/x. At x

=0 the division would error, so the program returns the limit value 1 in this case.

«→ x « IF 'x‹0' THEN x SIN x / ELSE 1 END » »

The following version uses IFTE algebraic syntax:

«→ x 'IFTE(x‹0,SIN(x)/x,1' »

Example: Two Conditional Actions. This program multiplies two numbers together if they’re both nonzero — otherwise, it returns the string “ZERO”.

Program:

Comments:

 

 

«

 

→ n1 n2

Creates the local variables.

«

Starts the defining procedure.

IF

Starts the test clause.

'n1‹0 AND n2‹0'

Tests n1 and n2.

THEN

If both numbers are nonzero, multiplies the two

n1 n2 *

values.

ELSE

Otherwise, returns the string ZERO.

"ZERO"

 

END

Ends the conditional.

»

Ends the defining procedure.

»

 

 

 

Example: Two Conditional Actions. This program tests if two numbers on the stack have the same value. If so, it drops one of the numbers and stores the other in variable V1 — otherwise, it stores the number from level 1 in V1 and the number from level 2 in V2.

Program:

Comments:

 

 

«

 

IF

For the test clause, copies the numbers in levels 1 and

DUP2

2 and tests if they have the same value.

SAME

 

THEN

For the true clause, drops one of the numbers and

DROP

stores the other in V1.

'V1' STO

 

ELSE

For the false clause, stores the level 1 number in V1

'V1' STO

and the level 2 number in V2.

'V2' STO

 

END

Ends the conditional structure.

»

 

`

Puts the program on the stack.

OTST K

Stores it in TST.

 

 

Enter the numbers 26 and 52, then execute TST to compare their values. Because the two numbers aren’t equal, the VAR menu now contains two new variables V1 and V2.

26 `52 J%TST%

1-16 RPL Programming