CASE Statement [ SPL to HP C/XL Migration Guide ] MPE/iX 5.0 Documentation
SPL to HP C/XL Migration Guide
CASE Statement
Table 6-11. CASE Statement
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| case-statement: | switch-statement: |
| | |
| CASE [*] index OF | switch ( index ) |
| | |
| BEGIN | "{" |
| | |
| statement0 ; | case 0: statement0 break ; |
| | |
| statement1 | case 1: statement1 break ; |
| | |
| [;...][;] | [...] |
| | |
| END | [default: exception-statement] |
| | |
| | "}" |
| | |
---------------------------------------------------------------------------------------------
| | |
| The statements in the BEGIN-END clause are | The statements in the { } clause are |
| implicitly numbered from 0. They may be | explicitly numbered with case number |
| compound statements. | labels. number may be any integer |
| | constant, including a character constant. |
| | There may be multiple simple, structured, |
| | or compound statements between labels. |
| | |
| | The break statement is required to emulate |
| | the operation of the SPL CASE statement, |
| | except if the SPL statement contains a GOTO |
| | statement. |
| | |
---------------------------------------------------------------------------------------------
| | |
| index is evaluated and the corresponding | index is evaluated and execution transfers |
| statement in the BEGIN-END clause is | to the case label with the corresponding |
| executed. Then execution drops through to | number. Statements are executed in |
| the statement after the CASE statement. | sequential order from that point until |
| | (1) the end of the { } clause is reached, |
| | (2) a break statement is executed, or |
| | (3) a goto statement is executed. |
| | If (1) or (2) occurs, execution drops |
| | through to the statement after the switch |
| | statement. |
| | If (3) occurs, execution continues at the |
| | label specified. |
| | |
---------------------------------------------------------------------------------------------
| | |
| If index is out-of-range, execution simply | The optional default label can be used to |
| drops through to the statement after the | trap out-of-range index values. If it is |
| CASE statement. | omitted, execution simply drops through to |
| | the statement after the switch statement. |
| | |
---------------------------------------------------------------------------------------------
| | |
| The "*" option turns off bounds checking. | No equivalent. Just delete the "*". |
| | |
---------------------------------------------------------------------------------------------
Table 6-12. CASE Statement Examples
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| | |
| CASE N OF | switch (N) |
| BEGIN | { |
| A:=100; <<case 0, N=0>> | case 0: A=100; /* N==0 */ |
| ; <<case 1, N=1>> | break; |
| BEGIN <<case 2, N=2>> | case 1: break; /* N==1 */ |
| A:=90; | case 2: A=90; /* N==2 */ |
| B:=1; | B=1; |
| END; | break; |
| B:=100; <<case 3, N=3>> | case 3: B=100; /* N==3 */ |
| END; | break; |
| | } |
| | |
---------------------------------------------------------------------------------------------
| | |
| Case 1 is a null statement. It is required | Case 1 could be omitted entirely, since an |
| to fill out the range of values for N, even | index not represented by a case label |
| if N would never equal 1. | terminates the switch. |
| | |
| | To emulate the SPL operation, each case |
| | ends with a break statement to terminate |
| | the switch. |
| | |
| | Note that case 2 does not require braces |
| | around the two statements, although they |
| | could be used to clarify the BEGIN-END |
| | translation. |
| | |
---------------------------------------------------------------------------------------------
Again, please note the following:
In SPL, after each "case" of a CASE statement is executed, there is an
automatic transfer to the end of the CASE statement. In HP C/XL,
execution by default "falls through" to the next case. The break
statement causes control to transfer to the statement following the
switch statement, emulating SPL's action.
This and other features of the HP C/XL switch statement may afford
opportunities to simplify older SPL algorithms once the code has been
implemented in HP C/XL.
In the HP C/XL switch statement, if you include a case labelled default,
invalid indexes will transfer to this label. Using a default label is
good programming practice.
The HP C/XL case labels are simply entries into a series of statements.
They may occur in any order and there may be gaps in the numeric
sequence.
MPE/iX 5.0 Documentation