Contents | < Browse | Browse >
5J. select-case-statement (SELECT)
SELECT, CASE, DEFAULT, ENDSELECT
syntax: SELECT <var>
[ CASE <exp>
[ CASE <exp>
<statements> ] /* any number of these blocks */
builds a select-case block. Various expressions will be matched against
the variable, and only the first matching block executed. If nothing matches,
a default block may be executed.
WriteF('Gee, I just found a linefeed\n')
WriteF('Wow, this must be a tab!\n')
WriteF('Do you know this one: "\c" ?\n',character)
next to the old SELECT <var> which works on expressions in the CASE
statements, E has a SELECT <maxrange> OF <exp> which works with constants
and or ranges of constants in a CASE. not only is this for many applications
more powerful, it is also much faster for large numbers of cases (>5
usually), or if cases are equally probable. It assumes however, that all
CASEs lie within a small integer range from 0 TO n-1, where n is something
reasonable, for example 10, or 256 for characters.
SELECT 128 OF FgetC(stream)
CASE "\t"," "
CASE "0" TO "9"
CASE "A" TO "Z", "a" TO "z", "_"
WriteF('some other character\n')
DEFAULT will be hit not only for those for which there is no CASE,
but also for the chars that fall out of the range, i.e. 128 TO 255
(and >255, and <0).
note that speed costs: because this SELECT uses a jump-table
to quickly get at the right CASE, it'll use 2*<maxrange> bytes,
256 in this case.