Go to the first, previous, next, last section, table of contents.


Control-struct: case{ on: k1 ... on: k2 ... else: ... }
file: job.t
package: muf
status: alpha

This construct is a multiway branch corresponding to the Pascal case and C switch statements, except that the constants may be any values, including strings. (I've frequently wished that C allowed string keys in switch!) The MUF version also has the advantage of being able to return a value, unlike either the C or Pascal versions. (Ansi Forth has a multiway branch also, but its semantics seem twisted enough to cause continual problems for novice programmers; After some thought, I decided to break new ground.)

: xlt { $ -> $ } case{ on: 1 "one" on: 2 "two" else: "many" } ;
1 xlt
Stack: "one"
pop 2 xlt
Stack: "two"
pop 3 xlt
Stack: "many"

The case{ operator pops one value off the stack and compares it with all provided on: constants until it finds a match. If it finds a match, it executes the associated code clause; otherwise, it executes the else: code clause, if present.

The current MUF compilers produce code which simply scans all on: clauses in order until a match is found: It may be wise to put the most frequently used cases first if speed is an issue.

Note that, unlike C, the MUF case{ statement does not require that clauses be separated by break operators. This feature of C tends to cause obscure bugs, to puzzle newcomers (particularly those trained on Pascal), and to add unneeded verbosity: Try translating the above one-liner into C.

Go to the first, previous, next, last section, table of contents.