Available on: all targets
See also: CASE CASE ELSE ENDSELECT

SELECT CASE...CASE...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.

SYNTAX

 SELECT CASE expression
    CASE value1:
       ...
    CASE value2:
       ...
    ...
    [ CASE ELSE ]
       ...
 ENDSELECT


Legend
  • id : identifier
  • type : datatype
  • v : value
  • "..." : string
  • [...] : optional

EXAMPLE

 SELECT CASE number
   CASE 1
      PRINT "one!"
   CASE 2
      PRINT "two!"
   CASE ELSE
      PRINT "neither!"
 ENDSELCT


Used in:

ABBREVIATION: SlCsCsCsElEs

Join BASIC 10Liner Contest with ugBASIC!

An interesting competition is held at the beginning of each year: the BASIC 10Liner Contest. It is possible to use ugBASIC to participate in the next "BASIC10Liner" competition, in the following categories:

  • PUR-120 - A game in 10 lines of max 120 characters (w/abbrev.)
  • EXTREME-256 - A game in 10 lines of max 256 characters (w/abbrev.)
  • SCHAU - Any program in 10 lines of max 256 characters (w/abbrev.)
In order to reduce space you can use this abbreviation for this instruction:

SELECT CASE...CASE...CASE ELSE...ENDSELECT ↔ SlCsCsCsElEs

Any problem?

If you have found a problem with this keyword, if you think there is a bug or, more simply, you would like it to be improved, open an issue for this example on GitHub.
Thank you!

open an issue BACK TO KEYWORDS