To enter CASE 7 END in a program:
1.Press !°%BRCH% !%CASE% to enter CASE … THEN …END…END
2.For each additional
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
« 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 |
'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 |
x y * | Multiplies the two numbers together only if |
| AND returns true. |
END | Ends the |
» |
|
» |
|
|
|
The following program accomplishes the same task as the previous program:
«→ x y « IF 'x AND y' THEN x y * END » »
The
«→ x y « 'x AND y' 'x*y' IFT » »
RPL Programming