HTBasic Help
×
Menu
Index

SELECT ... CASE

Defines a CASE block structure.
 
 SELECT string-or-numeric-expression
 CASE case-expression
   statements
 [CASE ELSE]
   statements
 END SELECT
 
 
 
Usage:
10  SELECT Option$
20  CASE "B"
30    A=1
40  CASE "0" TO "9","y","n"
50    A=2
60  CASE ELSE
70    A=0
80  END SELECT
Example: SELECT CASE.BAS
Description:
The SELECT and END SELECT statements enclose a SELECT structure. The SELECT statement specifies a numeric or string expression. Within the SELECT structure, CASE statements introduce alternative program sections to be executed based on the value of the SELECT statement expression. Each CASE statement type must match the type of expression in the SELECT statement. 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.
The SELECT expression value is used to test against each CASE statement value or range of values. The program statements following the first CASE statement to match are executed. Execution then continues at the line following the END SELECT statement. If none of the CASE statements match and there is an optional CASE ELSE statement, the program statements following the CASE ELSE will be executed, otherwise the entire SELECT structure is skipped.
While doing so is not encouraged, jumping into a SELECT structure with a GOTO is legal. Program statements are executed normally until a CASE statement is encountered. Execution then continues at the line following the END SELECT statement.
If there is an expression evaluation error in either the SELECT statement or one of the CASE statements the SELECT statement 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 a SELECT statement. 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 the SELECT statement 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.
See Also: