Contents | Prev | Next | Index


Case Statements

The purpose of a case statement is to specify the selection and execution of a statement sequence according to the value of an expression. It starts by first evaluating the expression; then that statement sequence is executed whose case label list contains the obtained value. The case expression has to be a value of a basic type (except reals), of an enumeration or of a subrange type. Its type has to include all case label values, and the case expression and the case labels must be compatible. Case labels are constants, and no value may occur than once. If the case expression value does not match any label, the default statement sequence following symbol ELSE is executed if one exists. Otherwise, the program is aborted.

case_stmt       ::= switch_expr case_list else_case END
switch_expr     ::= CASE expr OF
case_list       ::= case_list | case
                ::= case
case            ::= case_label_list : stmt_seq
                ::= <empty>
case_label_list ::= case_label_list , case_label
                ::= case_label
case_label      ::= const_expr .. const_expr
                ::= const_expr
else_case       ::= ELSE stmt_seq
                ::= <empty>

Example:

CASE ch OF
  "A".."Z": ReadIdentifier
| "0".."9": ReadNumber
| "'",'"': ReadString
ELSE SpecialCharacter
END


Contents | Prev | Next | Index

Canterbury Modula-2 for Java  (Last documentation update Feb 8, 2000)
Copyright © 1998 Mill Hill & Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to mhc@webcom.com