Caseof - Case - Endcase

caseof expr

            …

case n

            …

endcase

The action of these control statements is similar to the FORTRAN-computed GOTO statement or C’s SWITCH statement. It allows control to be passed rapidly to one of several code segments, depending on the value of an index. The use of the keywords is illustrated:

caseof expr
    ; ................. default code here
    case i1
    ; ................. case i1 code here
    case i2
    ; ................. case i2 code here
    case i3
    ; ................. case i3 code here
endcase

The object expr following caseof can be any valid algebraic expression; The items i1, i2, i3, … can be integers or strings that are constant when the function is parsed. If the value of expr equals to i1 (for strings, the comparison is case insensitive), then control jumps to the statements following the case i1 statement; execution then continues until the next case statement is encountered. Control then jumps to the code following the endcase statement; there is no “fall-through” as in the C language. Similar jumps are executed if the value of expr equals i2, i3, and so on. If the value of expr does not equal the numbers associated with any of the case statements, then any code immediately following the caseof statement is executed, with a jump to endcase when the first case is encountered. If the value of expr is less than zero or greater than the greatest number associated with any of the cases, then an execution error is reported, and processing stops. The numbers n (e.g., i1, i2, i3) need not be sequential or contiguous, but no duplicate numbers may exist.

caseofendcase sections may be nested to any degree; there will be no conflict between case numbers in the different levels of nesting (e.g., several instances of case 5 may appear, provided that they are all associated with different nesting levels). The use of case statements allows rapid decisions to be made (much more quickly than for a series of if … endif statements).