To enter CASE 7 END in a program:

1.Press !°%BRCH% !%CASE% to enter CASE … THEN …END…END

2.For each additional test-clause, move the cursor after a test-clause END and press @%CASE% to enter THEN … END.

Conditional Examples

These examples illustrate conditional structures in programs.

Example: One Conditional Action. The programs below test the value in level 1 — if the value is positive, it’s made negative. The first program uses a command sequence as the test-clause:

« DUP IF 0 > THEN NEG END »

The value on the stack must be duplicated because the > command removes two arguments from the stack (0. and the copy of the value made by DUP).

The following version uses an algebraic as the test clause:

«→ x « x IF 'x>0' THEN NEG END » »

The following version uses the IFT command:

«DUP 0 > « NEG » IFT »

Example: One Conditional Action. This program multiplies two numbers if both are nonzero.

Program:

Comments:

 

 

«

 

→ x y

Creates local variables x and y containing

«

the two numbers from the stack.

 

IF

Starts the test-clause.

'x‹0'

Tests one of the numbers and leaves a test

 

result on the stack.

'y‹0'

Tests the other number, leaving another test

 

result on the stack.

AND

Tests whether both tests were true.

THEN

Ends the test-clause, starts the true-clause.

x y *

Multiplies the two numbers together only if

 

AND returns true.

END

Ends the true-clause.

»

 

»

 

 

 

The following program accomplishes the same task as the previous program:

«→ x y « IF 'x AND y' THEN x y * END » »

The test-clause 'x AND y' returns “true” if both numbers are nonzero. The following version uses the IFT command:

«→ x y « 'x AND y' 'x*y' IFT » »

RPL Programming 1-15