SELECT CASE expression CASE value1: ... CASE value2: ... ... [ CASE ELSE ] ... ENDSELECT
The SELECT CASE
command is part of the SELECT CASE…END SELECT
structure.
It allows you to execute different blocks of code depending on the value of a
variable or expression. In practice, it is like having a series of “cases” and
the program executes the code corresponding to the case that occurs.
The expression
is evaluated at the beginning of the control block,
and its value is compared to the values specified in the cases.
Each CASE
represents a possible value or a range of values
of the expression. Inside each case, you insert the instructions that
will be executed if the value of the expression matches that case.
The CASE ELSE
is optional, and it is executed if no previous
case is true. In 8-bit computers, the semantics of SELECT CASE
are
closely tied to data representation. Because registers and variables were
often limited to 8 bits, the values that could be compared in cases are
integers between 0 and 255, also if ugBASIC supports any integer type
Anyway, it allows the expression to be compared to any integer value but,
due to the 8-bit limitation, the range of values that could be
compared should be limited, to be effective, in terms of performances.
Using SELECT CASE
makes code clearer and easier to understand
than a series of nested IF…THEN…ELSE
statements. It can
be more efficient than a series of IF…THEN…ELSE
statements.
SELECT CASE number CASE 1 PRINT "one!" CASE 2 PRINT "two!" CASE ELSE PRINT "neither!" ENDSELCT
See also the following example files:
SlCsCsCsElEs
If you have encountered a problem using this command, if you think there is a bug or the explanation is unclear, please open an issue for this keyword on GitHub. Thank you!