Skip to main content

$Case Function

The $Case function evaluates an expression (its first argument), and returns the result that corresponds to the value of the expression (the rest of the arguments). The last argument can be a default result, returned if no other argument value matches the expression.

$Case can return literals, as shown in the first example, or it can return names of procedures or routines, when used as the argument of a Do, as in the second example.

SAMPLES>set surv = 3

SAMPLES>write $case( surv, 1:"Rich", 2:"Kelly", 3:"Rudy", 4:"Sue", :"")
Rudy
SAMPLES>set surv = 1

SAMPLES>do $case( surv, 1:celebrate^survivor() , :complain^survivor() )
Yippee! I won!
SAMPLES>

The survivor.mac code:

survivor ; celebrate or complain
celebrate() PUBLIC
    { Write !, "Yippee! I won!" }

complain() PUBLIC
    { write !, "Oh well, I lost." }
FeedbackOpens in a new tab