1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-06-29 11:40:38 +00:00
QB64-PE/internal/help/SELECT_CASE.txt
SteveMcNeill 33adc04fc4 Add temp folder to repo. It's necessary as well!
Just more initial setting on... nothing much to see here.
2022-04-28 13:39:56 -04:00

171 lines
8.3 KiB
Plaintext

[[SELECT CASE]] is used to determine the program flow by comparing the value of a variable to specific CASE values.
{{PageSyntax}}
:'''SELECT''' [EVERY]'''CASE''' {{Parameter|testExpression}}
::'''CASE''' {{Parameter|expressionList1}}
:::[statement-block1]
::['''CASE''' {{Parameter|expressionList2}}
:::[statement-block2]]...
::['''CASE ELSE'''
:::[statementblock-n]]
:'''END SELECT'''
* '''SELECT CASE''' evaluates {{Parameter|testExpression}} and executes the first matching [[CASE]] or [[CASE ELSE]] block and exits.
* '''SELECT EVERYCASE''' allows the execution of all matching [[CASE]] blocks from top to bottom or the [[CASE ELSE]] block.
* The literal, variable or expression {{Parameter|testExpression}} comparison can result in any string or numerical type.
* '''Note:''' A {{Parameter|testExpression}} variable value can be changed inside of true CASE evaluations in SELECT EVERYCASE.
* A {{Parameter|testExpression}} derived from an expression or [[FUNCTION]] will only be determined once at the start of the block execution.
* <span id="allCASES">Supports individual CASE values and ranges or lists of literal values as below:</span>
** '''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''' > casevalue: code {{text|''''case compares a value as <nowiki> =, <>, < or > </nowiki>'''}}
** '''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 {{Parameter|testExpression}} 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 one line.
* An '''[[underscore]]''' can be used anywhere after the code on one line to continue it to the next line in '''QB64'''.
{{PageExamples}}
''Example 1:'' SELECT CASE can use literal or variable [[STRING]] or numerical values in CASE comparisons:
{{CodeStart}} '' ''
{{Cl|INPUT}} "Enter a whole number value from 1 to 40: ", value
value1 = 10
value2 = 20
value3 = 30
{{Cl|SELECT CASE}} value
{{Cl|CASE}} value1: {{Cl|PRINT}} "Ten only"
{{Cl|CASE}} value1 {{Cl|TO}} value2: {{Cl|PRINT}} "11 to 20 only" '10 is already evaluated
{{Cl|CASE}} value1, value2, value3: {{Cl|PRINT}} "30 only" '10 and 20 are already evaluated
{{Cl|CASE IS}} > value2: {{Cl|PRINT}} "greater than 20 but not 30" '30 is already evaluated
{{Cl|CASE ELSE}}: {{Cl|PRINT}} "Other value" 'values less than 10
{{Cl|END SELECT}} '' ''
{{CodeEnd}}
: ''Explanation:'' The first true CASE is executed and SELECT CASE is exited. "Other value" 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}} "This will not be shown."
{{Cl|CASE}} 10
{{Cl|PRINT}} "This will not be shown."
{{Cl|CASE}} 50
{{Cl|PRINT}} "This will not be shown."
{{Cl|CASE}} 100
{{Cl|PRINT}} "This will be displayed when a equals 100"
{{Cl|PRINT}} "(and no other case will be checked)"
{{Cl|CASE}} 150
{{Cl|PRINT}} "This will not be shown."
{{Cl|CASE IS}} < 150
{{Cl|PRINT}} "This will not be shown as a previous case was true"
{{Cl|CASE}} 50 {{Cl|TO}} 150
{{Cl|PRINT}} "This will not be shown as a previous case was true"
{{Cl|CASE ELSE}}
{{Cl|PRINT}} "This will only print if it gets this far!"
{{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}} "This will not be shown."
{{Cl|CASE}} 10
{{Cl|PRINT}} "This will not be shown."
{{Cl|CASE}} 50
{{Cl|PRINT}} "This will not be shown."
{{Cl|CASE}} 100
{{Cl|PRINT}} "This will be displayed when a equals 100"
{{Cl|PRINT}} "(and other cases will be checked)"
{{Cl|CASE}} 150
{{Cl|PRINT}} "This will not be shown."
{{Cl|CASE IS}} < 150
{{Cl|PRINT}} "This will be shown as 100 is less than 150"
{{Cl|CASE}} 50 {{Cl|TO}} 150
{{Cl|PRINT}} "This will be shown as 100 is between 50 and 150"
{{Cl|CASE ELSE}}
{{Cl|PRINT}} "This will only print if no other CASE is true!"
{{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}} "Enter a letter, number or punctuation mark from the keyboard: ";
valu$ = {{Cl|INPUT$}}(1)
{{Cl|PRINT}} value$
value1$ = "A"
value2$ = "m"
value3$ = "z"
{{Cl|SELECT CASE}} value$
{{Cl|CASE}} value1$: {{Cl|PRINT}} "A only"
{{Cl|CASE}} value1$ {{Cl|TO}} value2$: {{Cl|PRINT}} "B to m" 'A is already evaluated
{{Cl|CASE}} value1$, value2$, value3$: {{Cl|PRINT}} "z only" 'A and m are already evaluated
{{Cl|CASE IS}} > value2$: {{Cl|PRINT}} "greater than m but not z" 'z is already evaluated
{{Cl|CASE ELSE}}: {{Cl|PRINT}} "other value" '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|>]] or [[Less Than|<]] and to [[SWAP]] values in [[arrays]] regardless of the string lengths.
''Example 5:'' EVERYCASE is used to draw sections of digital numbers in a simulated LED readout using numbers from 0 to 9:
{{CodeStart}}
{{Cl|SCREEN}} 12
DO
{{Cl|LOCATE}} 1, 1: {{Cl|INPUT}} "Enter a number 0 to 9: ", 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}} "E2R30F2G2L30H2BR5P12,12" 'top horiz
{{Cl|CASE}} 0, 4 {{Cl|TO}} 6, 8, 9: {{Cl|PSET}} (20, 20), 12
{{Cl|DRAW}} "F2D30G2H2U30E2BD5P12,12" 'left top vert
{{Cl|CASE}} 0, 2, 6, 8: {{Cl|PSET}} (20, 54), 12
{{Cl|DRAW}} "F2D30G2H2U30E2BD5P12, 12" 'left bot vert
{{Cl|CASE}} 2 {{Cl|TO}} 6, 8, 9: {{Cl|PSET}} (20, 54), 12
{{Cl|DRAW}} "E2R30F2G2L30H2BR5P12, 12" 'middle horiz
{{Cl|CASE}} 0 {{Cl|TO}} 4, 7 {{Cl|TO}} 9: {{Cl|PSET}} (54, 20), 12
{{Cl|DRAW}} "F2D30G2H2U30E2BD5P12,12" 'top right vert
{{Cl|CASE}} 0, 1, 3 {{Cl|TO}} 9: {{Cl|PSET}} (54, 54), 12
{{Cl|DRAW}} "F2D30G2H2U30E2BD5P12,12" 'bottom right vert
{{Cl|CASE}} 0, 2, 3, 5, 6, 8: {{Cl|PSET}} (20, 88), 12
{{Cl|DRAW}} "E2R30F2G2L30H2BR5P12,12" 'bottom horiz
{{Cl|CASE ELSE}}
{{Cl|LOCATE}} 20, 20: {{Cl|PRINT}} "Goodbye!"; num
{{Cl|END SELECT}}
{{Cl|LOOP}} {{Cl|UNTIL}} num > 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.'''
{{PageSeeAlso}}
* [[IF...THEN]], [[Boolean]]
{{PageNavigation}}
<