Q(uick)BASIC Statement: SELECT CASE
Quick View
SELECT CASE
A control flow statement that executes one of several statement blocks depending on the value of an expression
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- SELECT CASE testexpression
CASE expressionlist1[statementblock-1][CASE expressionlist2[statementblock-2]]…[CASE ELSE[statementblock-n]]END SELECT
Description/Parameter(s)
testexpression | Any numeric or string expression. |
expressionlist1 expressionlist2 |
One or more expressions to match testexpression. The IS keyword must precede any relational operators in an expression. |
statementblock-1 statementblock-2 statementblock-n |
One or more statements on one or more lines. |
- The expressionlist arguments can have any of these forms or a combination of them, separated by commas:
- expression[,expression]…
- expression TO expression
- IS relational-operator expression
expression Any numeric or string expression compatible with testexpression. relational-operator One of the following relational operators:
<, <=, >, >=, <>, or =.
Example
INPUT "Enter acceptable level of risk (1-5): ", Total
SELECT CASE Total
CASE IS >= 5
PRINT "Maximum risk and potential return."
PRINT "Choose stock investment plan."
CASE 2 TO 4
PRINT "Moderate to high risk and potential return."
PRINT "Choose mutual fund or corporate bonds."
CASE 1
PRINT "No risk, low return."
PRINT "Choose IRA."
END SELECT
See also:
Syntax
- SELECT CASE testexpression
CASE expressionlist1[statementblock-1][CASE expressionlist2[statementblock-2] …[CASE ELSE[statementblock-n]]END SELECT
Description/Parameter(s)
testexpression | Any numeric or string expression. |
statementblock-1, statementblock-2, statementblock-n |
The elements statementblock-1 to statementblock-n consist of any number of statements on one or more lines. |
expressionlist1, expressionlist2 |
These elements can have any of the three following forms: |
|
The following list describes the parts of an expressionlist: | ||
expression | Any numeric or string expression. The type of the expression must match the type of the testexpression. | |
relational-operator | Any of the following operators: | |
Symbol | Meaning | |
< | Less than | |
<= | Less than or equal to | |
> | Greater than | |
>= | Greater than or equal to | |
<> | Not equal | |
= | Equal |
If the testexpression matches the expressionlist associated with a CASE clause, then the statement block following that CASE clause is executed up to the next CASE clause or, for the last one, up to END SELECT. Control then passes to the statement following END SELECT.
If you use the TO keyword to indicate a range of values, the smaller value must appear first. For example, the statements associated with the line CASE -1 TO -5 are not executed if the testexpresssion is -4. The line should be written as CASE -5 TO -1.
You may use a relational operator only if the IS keyword appears. If CASE ELSE is used, its associated statements are executed only if the testexpression does not match any of the other CASE selections. It is a good idea to have a CASE ELSE statement in your SELECT CASE block to handle unforeseen testexpression values.
When there is no CASE ELSE statement, and no expression listed in the CASE clauses matches testexpression, program execution continues normally. No error occurs.
You may use multiple expressions or ranges in each CASE clause. For example, the following line is valid:
- CASE 1 TO 4, 7 TO 9, 11, 13, IS > MaxNumber%
You may also specify ranges and multiple expressions for strings:
- CASE "everything", "nuts" TO "soup", TestItem$
CASE matches strings that are exactly equal to everything, the current value of TestItem$, or that fall between nuts and soup in alphabetical order.
Strings are evaluated according to the ASCII values of their characters. Lower-case letters have larger ASCII values than upper-case, therefore
- nuts > Nuts > NUTS
If an expression appears in more than one CASE clause, only the statements associated with the first appearance of the expression are executed.
SELECT CASE statements may be nested. Each SELECT CASE statement must have a matching END SELECT statement.
Examples
In the following program, the SELECT CASE statement is used to take different actions based on the input value: XXX,
' Program demonstrates various forms of CASE items
INPUT "Enter acceptable level of risk (1-10): ", Total
SELECT CASE Total
CASE IS >= 10
PRINT "Maximum risk and potential return"
PRINT "Choose stock investment plan"
CASE 6 TO 9
PRINT "High risk and potential return"
PRINT "Choose corporate bonds"
CASE 2 TO 5
PRINT "Moderate risk and return"
PRINT "Choose mutual fund"
CASE 1
PRINT "No risk, low return"
PRINT "Choose IRA"
CASE ELSE
PRINT "RESPONSE OUT OF RANGE"
END SELECT
Sample Output:
Enter acceptable level of risk (1-10): 10 Maximum risk and potential return Choose stock investment plan Enter acceptable level of risk (1-10): 0 RESPONSE OUT OF RANGEIn the following program, the SELECT CASE statement is used to take different actions based on the ASCII value of a character.
' Function and control key constants
CONST ESC = 27, DOWN = 80, UP = 72, LEFT = 75, RIGHT = 77
CONST HOME = 71, ENDKEY = 79, PGDN = 81, PGUP = 73
DO
' Get a function or ASCII key
DO
Choice$ = INKEY$
LOOP WHILE Choice$ = ""
IF LEN(Choice$) = 1 THEN
' Handle ASCII keys
SELECT CASE ASC(Choice$)
CASE ESC
PRINT "Escape key"
END
CASE IS < 32, 127
PRINT "Control code"
CASE 30 TO 29
PRINT "Digit: "; Choice$
CASE 65 TO 90
PRINT "Uppercase letter: "; Choice$
CASE 97 TO 122
PRINT "Lowercase letter: "; Choice$
CASE ELSE
PRINT "Punctuation: "; Choice$
END SELECT
ELSE
' Convert 2-byte extended code to 1-byte ASCII code and handle
Choice$ = RIGHT$(Choice$, 1)
SELECT CASE Choice$
CASE CHR$(DOWN)
PRINT "DOWN arrow key"
CASE CHR$(UP)
PRINT "UP arrow key"
CASE CHR$(PGDN)
PRINT "PGDN key"
CASE CHR$(PGUP)
PRINT "PGUP key"
CASE CHR$(HOME)
PRINT "HOME key"
CASE CHR$(ENDKEY)
PRINT "END key"
CASE CHR$(RIGHT)
PRINT "RIGHT arrow key"
CASE CHR$(LEFT)
PRINT "LEFT arrow key"
CASE ELSE
BEEP
END SELECT
END IF
LOOP
Syntax
- SELECT CASE testexpression
CASE expressionlist1[statementblock-1][CASE expressionlist2statementblock-2]]…[CASE ELSE[statementblock-n]]END SELECT
Description/Parameter(s)
testexpression | Any numeric or string expression (see details below). | |
statementblock | One or more expressions with a type compatible with testexpression. The IS keyword must precede any relational operators in an expression. | |
expressionlist | Any number of statements on one or more lines. | |
The argument expressionlist can have any of these forms:
|
||
The following list describes the parts of expressionlist: | ||
expression | Any numeric or string expression. The type of the expression must be compatible with testexpression. (The type of expression will be coerced to the same type as testexpression. For example, if test- expression is an integer, expressionlist can contain a double-precision data type.) | |
relational-operator | < | Less than |
<= | Less than or equal to | |
> | Greater than | |
>= | Greater than or equal to | |
<> | Not equal to | |
= | Equal to |
Usage Notes
- If testexpression matches the expressionlist associated with a particular CASE clause, the statement block following that CASE clause is executed up to the next CASE clause or, for the last one, up to END SELECT. Control then passes to the statement following END SELECT.
- If you use the TO keyword to indicate a range of values, the smaller value must appear first. For example, the statements associated with the line:
- CASE -1 TO -5
are not executed if the testexpresssion is -4. The line should be written as:
- CASE 1 TO 4, 7 TO 9, 11, 13, IS > MaxNumber%
- You can also specify ranges and multiple expressions for strings:
- CASE "everything", "nuts" TO "soup", TestItem$
- CASE matches strings that are exactly equal to everything, the current value of TestItem$, or that fall between nuts and soup in alphabetical order.
- Strings are evaluated according to the ASCII values of their characters. Lowercase letters have larger ASCII values than uppercase, so this statement is true:
- nuts > Nuts > NUTS
- If an expression appears in more than one CASE clause, only the statements associated with the first appearance of the expression are executed.
- SELECT CASE statements can be nested. Each SELECT CASE statement must have a matching END SELECT statement.
Examples
These examples use the SELECT CASE statement to take different actions based on the input value.
'This example determines an investment strategy according to risk.
INPUT "Enter acceptable level of risk (1-10): ", Total
SELECT CASE Total
CASE IS >= 10
PRINT "Maximum risk and potential return."
PRINT "Choose stock investment plan."
CASE 6 TO 9
PRINT "High risk and potential return."
PRINT "Choose corporate bonds."
CASE 2 TO 5
PRINT "Moderate risk and return."
PRINT "Choose mutual fund."
CASE 1
PRINT "No risk, low return."
PRINT "Choose IRA."
CASE ELSE
PRINT "Response out of range."
END SELECT
Sample Output:
Enter acceptable level of risk (1-10): 10 Maximum risk and potential return. Choose stock investment plan. Enter acceptable level of risk (1-10): 0 Response out of range.This example uses the SELECT CASE statement to display messages based on the ASCII value of each input character.
'Function and control key constants.
CONST ESC = 27, DOWN = 80, UP = 72, LEFT = 75, RIGHT = 77
CONST HOME = 71, ENDKEY = 79, PGDN = 81, PGUP = 73
CLS
PRINT "Press Escape key to end."
DO
'Get a function or ASCII key.
DO
Choice$ = INKEY$
LOOP WHILE Choice$ = ""
IF LEN(Choice$) = 1 THEN
'Handle ASCII keys.
SELECT CASE ASC(Choice$)
CASE ESC
PRINT "Escape key"
END
CASE IS < 32, 127
PRINT "Control code"
CASE 48 TO 57
PRINT "Digit: "; Choice$
CASE 65 TO 90
PRINT "Uppercase letter: "; Choice$
CASE 97 TO 122
PRINT "Lowercase letter: "; Choice$
CASE ELSE
PRINT "Punctuation: "; Choice$
END SELECT
ELSE
'Convert 2-byte extended code to 1-byte ASCII code and handle.
Choice$ = RIGHT$(Choice$, 1)
SELECT CASE Choice$
CASE CHR$(DOWN)
PRINT "DOWN arrow key"
CASE CHR$(UP)
PRINT "UP arrow key"
CASE CHR$(PGDN)
PRINT "PGDN key"
CASE CHR$(PGUP)
PRINT "PGUP key"
CASE CHR$(HOME)
PRINT "HOME key"
CASE CHR$(ENDKEY)
PRINT "END key"
CASE CHR$(RIGHT)
PRINT "RIGHT arrow key"
CASE CHR$(LEFT)
PRINT "LEFT arrow key"
CASE ELSE
BEEP
END SELECT
END IF
LOOP