> ##

> ## 1.6 SWITCH..CASE..DEFAULT..ENDSWITCH

> The HLA language does not provide a selection statement similar to SWITCH in C/C++ or CASE in Pascal/Delphi. This omission was intentional; by leaving the SWITCH statement out of the language it is possible to demonstrate how to extend the HLA language by adding new control structures. In the chapters on Macros and the HLA Compile-time Language, this text will demonstrate how you can add your own statements, like SWITCH, to the HLA language. In the meantime, although HLA does not provide a SWITCH statement, the HLA Standard Library provides a macro that provides this capability for you. If you include the "hll.hhf" header file (which "stdlib.hhf" automatically includes for you), then you can use the SWITCH statement exactly as though it were a part of the HLA language.

> The HLA Standard Library SWITCH statement has the following syntax:

> Figure 1.6 Syntax for the SWITCH..CASE..DEFAULT..ENDSWITCH Statement

> Like most HLA high level language statements, there are several restrictions on the SWITCH statement. First of all, the SWITCH clause does not allow a general expression as the selection value. The SWITCH clause will only allow a value in a 32-bit general purpose register. In general you should only use EAX, EBX, ECX, EDX, ESI, and EDI since EBP and ESP are reserved for special purposes.

> The second restriction is that the HLA SWITCH statement supports a maximum of 256 different case values. Few SWITCH statements use anywhere near this number, so this shouldn't prove to be a problem. Note that each CASE in Figure 1.6 allows a constant list. This could be a single unsigned integer value or a comma separated list of values, e.g.,

>     		case( 10 )

> -or-

>     		case( 5, 6, 8 )

> Each value in the list of constants counts as one case constant towards the maximum of 256 possible constants. So the second CASE clause above contributes three constants towards the total maximum of 256 constants.

> Another restriction on the HLA SWITCH statement is that the difference between the largest and smallest values in the case list must be 1,024. Therefore, you cannot have CASEs (in the same SWITCH statement) with values like 1, 10, 100, 1,000, and 10,000 since the difference between the smallest and largest values, 9999, exceeds 1,024.

> The DEFAULT section, if it appears in a SWITCH statement, must be the last section in the SWITCH statement. If no DEFAULT section is present and the value in the 32-bit register does not match one of the CASE constants, then control transfers to the first statement following the ENDSWITCH clause.

> Here is a typical example of a SWITCH..ENDSWITCH statement:

>     	switch( eax )

>     		case( 1 )

>     			stdout.put( "Selection #1:" nl );

>     			<< Code for case #1 >>

>     		case( 2, 3 )

>     			stdout.put( "Selections (2) and (3):" nl );

>     			<< code for cases 2 & 3 >>

>     		case( 5,6,7,8,9 )

>     			stdout.put( "Selections (5)..(9)" nl );

>     			<< code for cases 5..9 >

>     		default

>     			stdout.put( "Selection outside range 1..9" nl );

>     			<< default case code >>

>     	endswitch;

> The SWITCH statement in a program lets your code choose one of several different code paths depending upon the value of the case selection variable. Among other things, the SWITCH statement is ideal for processing user input that selects a menu item and executes different code depending on the user's selection.

> Later in this volume you will see how to implement a SWITCH statement using low-level machine instructions. Once you see the implementation you will understand the reasons behind these limitations in the SWITCH statement. You will also see why the CASE constants must be constants and not variables or registers.

> ## 1.7 Putting It All Together

> This chapter completes the discussion of the high level control structures built into the HLA language or provided by the HLA Standard Library (i.e., SWITCH). First, this chapter gave a complete discussion of the TRY..ENDTRY and RAISE statements. Although Volume One provided a brief discussion of exception handling and the TRY..ENDTRY statement, this particular statement is too complex to fully describe earlier in this text. This chapter completes the discussions of this important statement and suggests ways to use it in your programs that will help make them more robust.

> After discussing TRY..ENDTRY and RAISE, this chapter discusses the EXIT and EXITIF statements and describes how to use them to prematurely exit a procedure or the program. This chapter also discusses the BEGIN..END block and describes how to use the EXIT and EXITIF statements to exit such a block. These statements provide a structured GOTO (JMP) in the HLA language.

> Although you will not use them as frequently as the BREAK and BREAKIF statements, the CONTINUE and CONTINUEIF statements are helpful once in a while for jumping over the remainder of a loop body and starting the next loop iteration. This chapter discusses the syntax of these statements and warns against overusing them.

> This chapter concludes with a discussion of the SWITCH/CASE/DEFAULT/ENDCASE statement. This statement isn't actually a part of the HLA language - instead it is provided by the HLA Standard Library as an example of how you can extend the language. If you would like details on extending the HLA language yourself, see the chapter on "Domain Specific Languages."