SELECT CASE
From QB64 Wiki
SELECT CASE is used to determine the program flow by comparing the value of a variable to specific values.
- SELECT CASE variable
- 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:
- 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: