The 2011 Oberon Language Report does not explicitly specify implementation details of CASE statements such as:
- What is the valid range of values for CASE labels?
- What happens when a label appears more than once in the same CASE statement?
- What happens if the CASE selector references a label that does not exist?
- Valid CASE labels are in the range 0..255 for INTEGERs and 0X..0FFX for strings.
- Duplicate CASE labels are reported as compile-time errors.
- A reference to a missing label results in a runtime error and program termination.
For example, when included in a module, the following code snippet should compile and run without errors on an Astrobe v4 system:
Code: Select all
PROCEDURE ToUpperCase(VAR ch: CHAR);
BEGIN
IF (ch >= "a") & (ch <= "z") THEN
ch := CHR(ORD(ch) - ORD("a") + ORD("A"))
END
END ToUpperCase;
PROCEDURE SoundexCode(ch: CHAR): INTEGER;
VAR
value: INTEGER;
BEGIN
ToUpperCase(ch);
IF (ch < "A") OR (ch > "Z") THEN
value := 0
ELSE
CASE ch OF
"A", "E", "I", "O", "U", "H", "W", "Y":
value := 0 |
"B", "F", "P", "V":
value := 1 |
"C", "G", "J", "K", "Q", "S", "X", "Z":
value := 2 |
"D", "T":
value := 3 |
"L":
value := 4 |
"M", "N":
value := 5 |
"R":
value := 6
END
END;
RETURN value
END SoundexCode;
- The case labels are naturally integers or characters
- The smallest case label is close to zero or 0X.
- The case labels are relatively contiguous
- There are a large (i.e. more than half-a-dozen) number of cases
- All cases have similar probabilities of occurrence
In some cases a hybrid combination of CASE and IF statements can result in a good compromise between readability, efficiency and memory usage.