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


{{PageSyntax}}
:::'''SELECT''' [EVERY]'''CASE''' ''checkvalue''
::::[[CASE]]
::::[[CASE IS]]
::::[[CASE ELSE]]
:::'''[[END SELECT]]''' 



* '''SELECT CASE''' evaluates a comparison ''checkvalue'' and executes the first matching [[CASE]] or [[CASE ELSE]] and exits.
* '''SELECT EVERYCASE''' allows the execution of all matching [[CASE]] statements from top to bottom or [[CASE ELSE]].
* The literal or variable or expression ''checkvalue'' comparison can result in any string or numerical type. 
* '''Note:''' A ''checkvalue'' variable value can be changed inside of true CASE evaluations in SELECT EVERYCASE. 
* A ''checkvalue'' derived from an expression or [[FUNCTION]] will only be determined once at the start of the block execution.
* Supports individual CASE values and ranges or lists of literal values as below:
:* [[CASE]] casevalue: code {{text|''''case compares one numerical or text value'''}}
:* [[CASE]] casevalue1 [[TO]] casevalue2: code {{text|''''case compares a range of values '''}}
:* [[CASE]] casevalue1, casevalue2, casevalue3: code {{text|''''case compares a list of values separated by commas'''}}
:* [[CASE IS]] &gt; casevalue: code {{text|''''case compares a value as &lt;nowiki&gt; =, &lt;&gt;, &lt; or &gt; &lt;/nowiki&gt;'''}} 
:* [[CASE ELSE]]: code {{text|''''bottom case statement executes only when no other CASE is executed.}}'''
* The CASE values should cover the normal ranges of the comparison ''variable'' values. 
* Use [[CASE ELSE]] before [[END SELECT]] if an alternative is necessary when no other case matches.
* CASEs should be listed in an ascending or descending values for best and fastest results.
* [[STRING]] comparisons will be based on their respective [[ASCII]] code values where capital letters are valued less than lower case.
* Use SELECT CASE when [[IF...THEN]] statements get too long or complicated.
* SELECT CASE and EVERYCASE statement blocks must '''always''' be ended with [[END SELECT]]!
* Use '''[[colon]]s''' 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 1:'' SELECT CASE can use literal or variable [[STRING]] or numerical values in CASE comparisons:
{{CodeStart}} '' ''
{{Cl|INPUT}} &quot;Enter a whole number value from 1 to 40: &quot;, value
value1 = 10
value2 = 20
value3 = 30

{{Cl|SELECT CASE}} value
  {{Cl|CASE}} value1: {{Cl|PRINT}} &quot;Ten only&quot;
  {{Cl|CASE}} value1 {{Cl|TO}} value2: {{Cl|PRINT}} &quot;11 to 20 only&quot; '10 is already evaluated
  {{Cl|CASE}} value1, value2, value3: {{Cl|PRINT}} &quot;30 only&quot; '10 and 20 are already evaluated
  {{Cl|CASE IS}} &gt; value2: {{Cl|PRINT}} &quot;greater than 20 but not 30&quot; '30 is already evaluated
  {{Cl|CASE ELSE}}: {{Cl|PRINT}} &quot;Other value&quot; 'values less than 10
{{Cl|END SELECT}} '' ''
{{CodeEnd}}
: ''Explanation:'' The first true CASE is executed and SELECT CASE is exited. &quot;Other value&quot; is printed for values less than 10.


''Example 2:'' SELECT CASE will execute the first CASE statement that is true and ignore all CASE evaluations after that:
{{CodeStart}}
a = 100
{{Cl|SELECT CASE}} a          'designate the value to compare
  {{Cl|CASE}} 1, 3, 5, 7, 9
    {{Cl|PRINT}} &quot;This will not be shown.&quot;
  {{Cl|CASE}} 10
    {{Cl|PRINT}} &quot;This will not be shown.&quot;
  {{Cl|CASE}} 50
    {{Cl|PRINT}} &quot;This will not be shown.&quot;
  {{Cl|CASE}} 100
    {{Cl|PRINT}} &quot;This will be displayed when a equals 100&quot;
    {{Cl|PRINT}} &quot;(and no other case will be checked)&quot;
  {{Cl|CASE}} 150
    {{Cl|PRINT}} &quot;This will not be shown.&quot;
  {{Cl|CASE IS}} &lt; 150
    {{Cl|PRINT}} &quot;This will not be shown as a previous case was true&quot;
  {{Cl|CASE}} 50 {{Cl|TO}} 150
    {{Cl|PRINT}} &quot;This will not be shown as a previous case was true&quot;
  {{Cl|CASE ELSE}}
   {{Cl|PRINT}} &quot;This will only print if it gets this far!&quot;
{{Cl|END SELECT}} '' ''
{{CodeEnd}}
{{OutputStart}}This will be displayed when a equals 100
(and no other case will be checked)
{{OutputEnd}}
:''Explanation:'' The first case where a value is true is shown, the remainder are skipped. Try changing the value of ''a''.


''Example 3:'' Same as Example 2 but, SELECT EVERYCASE will execute every CASE statement that is true.
{{CodeStart}}
a = 100
{{Cl|SELECT CASE|SELECT EVERYCASE}} a          'designate the value to compare
  {{Cl|CASE}} 1, 3, 5, 7, 9
    {{Cl|PRINT}} &quot;This will not be shown.&quot;
  {{Cl|CASE}} 10
    {{Cl|PRINT}} &quot;This will not be shown.&quot;
  {{Cl|CASE}} 50
    {{Cl|PRINT}} &quot;This will not be shown.&quot;
  {{Cl|CASE}} 100
    {{Cl|PRINT}} &quot;This will be displayed when a equals 100&quot;
    {{Cl|PRINT}} &quot;(and other cases will be checked)&quot;
  {{Cl|CASE}} 150
    {{Cl|PRINT}} &quot;This will not be shown.&quot;
  {{Cl|CASE IS}} &lt; 150
    {{Cl|PRINT}} &quot;This will be shown as 100 is less than 150&quot;
  {{Cl|CASE}} 50 {{Cl|TO}} 150
    {{Cl|PRINT}} &quot;This will be shown as 100 is between 50 and 150&quot;
  {{Cl|CASE ELSE}}
   {{Cl|PRINT}} &quot;This will only print if no other CASE is true!&quot;
{{Cl|END SELECT}} '' ''
{{CodeEnd}}
{{OutputStart}}This will be displayed when a equals 100
(and other cases will be checked)
This will be shown as 100 is less than 150
This will be shown as 100 is between 50 and 150
{{OutputEnd}}
:''Explanation:'' [[CASE ELSE]] will only execute if no other CASE was true. See Example 5 for more usages.


''Example 4:'' SELECT CASE evaluates string values by the [[ASC]] code value according to [[ASCII]].
{{CodeStart}} '' ''
{{Cl|PRINT}} &quot;Enter a letter, number or punctuation mark from the keyboard: &quot;;
valu$ = {{Cl|INPUT$}}(1)
{{Cl|PRINT}} value$
value1$ = &quot;A&quot;
value2$ = &quot;m&quot;
value3$ = &quot;z&quot;

{{Cl|SELECT CASE}} value$
  {{Cl|CASE}} value1$: {{Cl|PRINT}} &quot;A only&quot;
  {{Cl|CASE}} value1$ {{Cl|TO}} value2$: {{Cl|PRINT}} &quot;B to m&quot; 'A is already evaluated
  {{Cl|CASE}} value1$, value2$, value3$: {{Cl|PRINT}} &quot;z only&quot; 'A and m are already evaluated
  {{Cl|CASE IS}} &gt; value2$: {{Cl|PRINT}} &quot;greater than m but not z&quot; 'z is already evaluated
  {{Cl|CASE ELSE}}: {{Cl|PRINT}} &quot;other value&quot; 'key entry below A including all numbers
{{Cl|END SELECT}} '' ''
{{CodeEnd}}
: ''Notes:'' [[STRING]] values using multiple characters will be compared by the [[ASCII]] code values sequentially from left to right. Once the equivalent code value of one string is larger than the other the evaluation stops. This allows string values to be compared and sorted alphabetically using [[Greater Than|&gt;]] or [[Less Than|&lt;]] and to [[SWAP]] values in [[arrays]] irregardless of the string lengths.


''Example 5:'' EVERYCASE allows sections of digital numbers to be drawn in a simulated LED readout using numbers from 0 to 9:
{{CodeStart}}
{{Cl|SCREEN}} 12
DO
  {{Cl|LOCATE}} 1, 1: {{Cl|INPUT}} &quot;Enter a number 0 to 9: &quot;, num
  {{Cl|CLS}}
  {{Cl|SELECT CASE|SELECT EVERYCASE}} num
    {{Cl|CASE}} 0, 2, 3, 5 {{Cl|TO}} 9: {{Cl|PSET}} (20, 20), 12
      {{Cl|DRAW}} &quot;E2R30F2G2L30H2BR5P12,12&quot; 'top horiz
    {{Cl|CASE}} 0, 4 {{Cl|TO}} 6, 8, 9: {{Cl|PSET}} (20, 20), 12
      {{Cl|DRAW}} &quot;F2D30G2H2U30E2BD5P12,12&quot; 'left top vert
    {{Cl|CASE}} 0, 2, 6, 8: {{Cl|PSET}} (20, 54), 12
      {{Cl|DRAW}} &quot;F2D30G2H2U30E2BD5P12, 12&quot; 'left bot vert
    {{Cl|CASE}} 2 {{Cl|TO}} 6, 8, 9: {{Cl|PSET}} (20, 54), 12
      {{Cl|DRAW}} &quot;E2R30F2G2L30H2BR5P12, 12&quot; 'middle horiz
    {{Cl|CASE}} 0 {{Cl|TO}} 4, 7 {{Cl|TO}} 9: {{Cl|PSET}} (54, 20), 12
      {{Cl|DRAW}} &quot;F2D30G2H2U30E2BD5P12,12&quot; 'top right vert  
    {{Cl|CASE}} 0, 1, 3 {{Cl|TO}} 9: {{Cl|PSET}} (54, 54), 12
      {{Cl|DRAW}} &quot;F2D30G2H2U30E2BD5P12,12&quot; 'bottom right vert
    {{Cl|CASE}} 0, 2, 3, 5, 6, 8: {{Cl|PSET}} (20, 88), 12
      {{Cl|DRAW}} &quot;E2R30F2G2L30H2BR5P12,12&quot; 'bottom horiz
    {{Cl|CASE ELSE}} 
      {{Cl|LOCATE}} 20, 20: {{Cl|PRINT}} &quot;Goodbye!&quot;; num
  {{Cl|END SELECT}}
{{Cl|LOOP}} {{Cl|UNTIL}} num &gt; 9 
{{CodeEnd}}
: '''Note:''' [[CASE ELSE]] will only execute if no other CASE is true! Changing the comparison value in a CASE may affect later CASE evaluations! '''Beware of duplicate variables inside of  cases affecting the comparison values and remaining cases!'''


''See also:'' 
* [[CASE]], [[CASE IS]]
* [[CASE ELSE]]
* [[IF...THEN]], [[Boolean]]


{{PageNavigation}}