SELECT CASE - QB64 Wiki

SELECT CASE

From QB64 Wiki

Revision as of 20:01, 21 August 2012 by Clippy (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

SELECT CASE is used to determine the program flow by comparing the value of a variable to specific values.


Syntax:

SELECT CASE variable
' CASE
' CASE IS
' CASE ELSE
END SELECT


  • Variable can be any literal string or numerical type. Can only evaluate ONE value or calculation.
  • Use SELECT CASE when IF statements get too long or complicated.
  • The CASE comparisons should cover the normal ranges of the variable values(use CASE ELSE if necessary).
  • CASEs should be listed in an ascending or descending values for best and fastest results.
  • The routine will execute code in the FIRST True CASE statement and exit the procedure.
  • Supports individual CASE values and ranges or lists of values as below:
  • CASE value
  • CASE value1 TO value2
  • CASE value1, value2, value3
  • CASE IS value1 > value2
  • CASE ELSE
  • SELECT CASE statements must always be ended with END SELECT!
  • Use colons to execute multiple statements in a one line statement. You cannot use AND for multiple statements!
  • An underscore can be used anywhere after the code on one line to continue it to the next line in QB64 ONLY.


Example:

a = 100 SELECT CASE a 'designate the value to compare CASE 1, 3, 5, 7, 9 PRINT "This will not be shown." CASE 10 PRINT "This will not be shown." CASE 50 PRINT "This will not be shown." CASE 100 PRINT "This will be displayed when a equals 100)" PRINT "(and no other case will be checked)" CASE 150 PRINT "This will not be shown." CASE IS < 150 PRINT "This will not be shown as a previous case was true" CASE 50 TO 150 PRINT "This will not be shown as a previous case was true" CASE ELSE PRINT "This will only print if it gets this far!" END SELECT

This will be displayed when a equals 100) (and no other case will be checked)

Explanation: The first case where a value is true is shown, the remainder are skipped. Try changing the value of 'a' to different numbers!


See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page