TheSELECTandEND SELECTstatements enclose aSELECTstructure. TheSELECTstatement specifies a numeric or string expression. Within theSELECTstructure,CASEstatements introduce alternative program sections to be executed based on the value of theSELECTstatement expression. EachCASEstatement type must match the type of expression in theSELECTstatement. If a case-expression contains multiple values, the values are tested from left to right until a match is found. Any remaining expressions are not tested.
TheSELECTexpression value is used to test against eachCASEstatement value or range of values. The program statements following the firstCASEstatement to match are executed. Execution then continues at the line following theEND SELECTstatement. If none of theCASEstatements match and there is an optionalCASE ELSEstatement, the program statements following theCASE ELSEwill be executed, otherwise the entireSELECTstructure is skipped.
While doing so is not encouraged, jumping into aSELECTstructure with aGOTOis legal. Program statements are executed normally until aCASEstatement is encountered. Execution then continues at the line following theEND SELECTstatement.
If there is an expression evaluation error in either theSELECTstatement or one of theCASEstatements theSELECTstatement line number is reported with the error value.
Implementing ELSE IF
Although HTBasic does not have an explicit ELSE IF statement, it is possible to accomplish the same thing using aSELECTstatement. Suppose you wish an ELSE IF construct like this:
10 IF X<-1 THEN
20 !do something here
30 ELSE IF Z=0 THEN
40 !do something else here
50 ELSE
60 !and something else here
70 END IF
This example can be accomplish using theSELECTstatement as follows:
5 SELECT 1
10 CASE X<-1
20 !do something here
30 CASE Z=0
40 !do something else here
50 CASE ELSE
60 !and something else here
70 END SELECT
Line 5 states that the first case which evaluates to one will be executed. Since the result of a logical operator is 0 or 1, the first case with a logical expression that evaluates true will be executed.