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; when evaluated, it will be converted to an integer. The items i1, i2, i3, … must be integers (not symbols) in the range 0 to 255. If the value of expr equals i1, 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). However, the penalty is that some memory is consumed; the amount of memory used depends on the maximum numerical value associated with the case statements. The memory consumed is one plus the maximum case number in double-words (four-byte units).