A high-level case command is provided for selecting alternatives with multiple possibilities. This command is easier to read than deeply-nested ifthen commands.
Table 4-29 lists the conditional case commands.
Table 4-29 case Statement Commands
Command |
Stack Diagram |
Description |
---|---|---|
case |
( selector -- selector ) |
Begin a caseendcase conditional. |
endcase |
( selector -- ) |
Terminate a caseendcase conditional. |
endof |
( -- ) |
Terminate an ofendof clause in a case...endcase |
of |
( selector test-value -- selector | {empty} ) |
Begin an ofendof clause in a case conditional. |
Here is a simple example of a case command:
ok : testit ( testvalue -- ) ] case ] 0 of ." It was zero " endof ] 1 of ." It was one " endof ] ff of ." Correct " endof ] -2 of ." It was minus-two " endof ] ( default ) ." It was this value: " dup . ] endcase ." All done." ; ok ok 1 testit It was one All done. ok ff testit Correct All done. ok 4 testit It was this value: 4 All done. ok
The (optional) default clause can use the test value which is still on the stack, but should not remove it (use the phrase "dup ." instead of "."). A successful of clause automatically removes the test value from the stack.