SELECT [ HP Business BASIC/XL Reference Manual ] MPE/iX 5.0 Documentation
HP Business BASIC/XL Reference Manual
SELECT
The SELECT, CASE, CASE ELSE, and END SELECT statements define a construct
that executes one set of statements, depending on the value of an
expression.
Syntax
[CASE case_descriptor] [CASE ELSE]
[[stmt] ] [[stmt] ]
SELECT select_expr [. ]...[. ] END SELECT
[. ] [. ]
[. ] [. ]
Parameters
select_expr An expression evaluated and compared to the case
descriptor's. If the value is numeric, it is converted
to the default numeric type first.
case_descriptor One of the following:
* num_item [, num_item]...
* str_item [, str_item]...
num_item One of the following:
* num_lit [TO num_lit [EXCLUSIVE]
* {<,<=,>=,>} num_lit
EXCLUSIVE If specified, the range excludes the two specified
num_lits. For example, CASE 10 TO 20 EXCLUSIVE excludes
both 10 and 20.
str_item One of the following:
* str_lit[TO str_lit [EXCLUSIVE]
* {<,<=,>=,>} str_lit
Each case_descriptor must be a numeric literal if
select_expr evaluates to a numeric value and a string
literal if it evaluates to a string value.
If the select_expr value is equal to one of the
specified case_descriptor literals or is within the
range specified in the case_descriptor, then the case
clause associated with the case_descriptor is executed.
stmt Program line. It is executed if select_expr fits the
associated case_descriptor. Each sequence of program
lines between a CASE and either the next CASE or an END
SELECT constitutes a case clause.
Examples
100 SELECT Number
110 CASE < 0 !If Number is negative.
120 READ Number
130 CASE 0 !If Number is zero
140 LET Number=Default
150 CASE 1 TO 10 !If Number is 1 - 10
160 PRINT Number
170 GOTO Routine1
180 CASE 10 TO 20 EXCLUSIVE !If Number is 11 - 19 (due to the EXCLUSIVE keyword)
190 PRINT Number
200 Number=2*Number
210 GOSUB Routine2
220 CASE 20,30,40 !If Number is 20, 30 or 40
230 PRINT Number
240 GOSUB 450
250 CASE ELSE !If Number is any other value
260 LET Number=Default
270 END SELECT
HP Business BASIC/XL evaluates the select expression and compares its
value with each case descriptor's starting with the first and proceeding
in line number order, until a case descriptor describes the value or
every case descriptor has been checked.
When a case descriptor describes the value, the statements in its case
clause are executed; then, control is transferred to the statement
following the END SELECT statement. If more than one case descriptor
describes the value, only the first one's case clause is executed.
If no case descriptor describes the value, the CASE ELSE clause is
executed, if there is one. If there is no CASE ELSE clause, control is
transferred to the statement following the END SELECT statement.
The string value of a select_expr is compared with the quoted string
literals character by character. The following code segment outputs In
the first half of the dictionary.
100 Str_var$ = "dog"
110 SELECT Str_var$
120 CASE "a" To "m"
130 PRINT "In the first half of the dictionary."
140 CASE "dog"
150 PRINT "my pet."
160 END SELECT
10 SELECT A+B
20 CASE < 0 !A+B < 0
21 PRINT "Negative"
22 STOP
30 CASE 0 !A+B = 0
31 PRINT "Zero"
32 LET X=Default
40 CASE 1 TO 10 !1 <= A+B <= 10
41 PRINT "1 thru 10"
42 GOSUB Routine1
50 CASE 10 TO 20 EXCLUSIVE !10 < A+B < 20
51 PRINT "Between 10 & 20"
52 GOSUB Routine2
60 CASE 20,22,24 !A+B = 20, 22, or 24
61 PRINT "Special Case #1"
62 GOSUB Spec_case1
70 CASE 21,23,25 !A+B = 21, 23, or 25
72 PRINT "Special Case #2"
73 GOSUB Spec_case2
80 CASE > 30 !A+B > 30
81 PRINT "Over 30 by:"
82 PRINT (A+B)-30
83 STOP
90 CASE ELSE !26 <= A+B <= 30
91 PRINT "26 thru 30"
92 GOSUB Routine3
100 END SELECT
SELECT constructs can be nested.
100 SELECT Color1$ !Start outer construct
110 CASE "red", "blue", "yellow" !First case in outer construct
120 GOSUB Primary
130 SELECT Color1$ !Start first inner construct
140 CASE "red" !Case in first inner construct
150 PRINT "RED"
160 PRINT "ORANGE,PURPLE"
170 CASE "blue" !Case in first inner construct
180 PRINT "BLUE"
190 PRINT "PURPLE,GREEN"
200 CASE "yellow" !Case in first inner construct
210 PRINT "YELLOW"
220 PRINT "ORANGE,GREEN"
230 END SELECT !End first inner construct
240 CASE "green","purple","orange" !Second case in outer construct
250 GOSUB Secondary
260 SELECT Color2$ !Start second inner construct
270 CASE "green" !Case in second inner construct
280 PRINT "YELLOW+BLUE"
290 CASE "purple" !Case in second inner construct
300 PRINT "BLUE+RED"
310 CASE "orange" !Case in second inner construct
320 PRINT "RED+YELLOW"
330 END SELECT !End second inner construct
340 END SELECT !End outer construct
Entering the middle of a SELECT construct from a statement other than the
SELECT statement is considered to be a bad programming practice, and is
not recommended. However, if control is transferred to a statement that
is in the middle of a SELECT construct, execution proceeds in line number
order starting with that statement. When a CASE, CASE ELSE, or END
SELECT statement is reached, control is transferred to the statement
following the END SELECT statement.
Control can be transferred out of a SELECT construct and then back by
using a GOSUB or CALL statement.
100 SELECT T
110 REM Clause 1
120 CASE < 0
121 CALL Sub1 !Jump out of SELECT construct
122 PRINT T !Return to construct from 520
130 REM Clause 2
131 CASE 0
132 GOSUB 300 !Jump out of construct
133 PRINT 2*T !Return to construct from 310
134 PRINT T
140 REM Clause 3
141 CASE > 0
142 GOSUB 400 !Jump out of construct
150 END SELECT !Return to construct from 410
160 STOP
300 REM Do anything !Arrive from Clause 2, line 132
310 RETURN !Return to Clause 2, line 133
400 REM Do anything !Arrive from Clause 3, line 142
410 RETURN !Return to Clause 3, line 122
500 SUB Sub1 !Called from Clause 1 line 121
510 REM In procedure
520 SUBEND !Return to clause 1 line 122
999 END
MPE/iX 5.0 Documentation