OpenBoot 2.x Command Reference Manual

The case Statement

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-22 lists the conditional case commands.

Table 4-22 case Statement Commands

Command  

Stack Diagram 

Description 

case

( selector -- selector )  

Begin a caseendcase conditional.

endcase

( selector | {empty} -- ) 

Terminate a caseendcase conditional.

endof

( -- ) 

Terminate an ofendof clause within a case...endcase

of

( selector test-value -- selector | {empty} ) 

Begin an ofendof clause within 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 


Note -

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.