User Tools

Site Tools


ugbasic:user:kw:select_case...case...case_else...endselect
Translations of this page:


ugBASIC User Manual

SELECT CASE...CASE...CASE ELSE...ENDSELECT ✓

SYNTAX

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

PURPOSE

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.

EXAMPLE

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

See also the following example files:

ABBREVIATION

 SlCsCsCsElEs

AVAILABLE ON

  • Atari (400/800)
  • Atari XL/XE
  • Commodore 128 (MOS 8502)
  • Commodore 128 (Zilog Z80)
  • Commodore 64
  • Commodore 64+REU
  • TRS-80 Color Computer
  • TRS-80 Color Computer 3
  • ColecoVision
  • Amstrad CPC 664
  • Dragon 32
  • Dragon 64
  • Thomson MO5
  • Thomson MO5
  • Olivetti Prodest PC128
  • Commodore PLUS/4
  • SEGA SC-3000
  • SEGA SG-1000
  • VG-5000
  • Commodore VIC-20
  • ZX Spectrum 48

SEE ALSO

ANY PROBLEM?

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!

POWERED BY