mirror of
https://github.com/QB64Official/qb64.git
synced 2024-08-30 10:40:15 +00:00
86 lines
3.6 KiB
Text
86 lines
3.6 KiB
Text
|
[[CASE]] is used within a [[SELECT CASE]] block to specify a conditional value of the compared variable.
|
||
|
|
||
|
|
||
|
{{PageSyntax}}
|
||
|
|
||
|
:: '''CASE comparison_value(s)'''[:] execute_code_line_or_block
|
||
|
|
||
|
|
||
|
{{PageDescription}}
|
||
|
|
||
|
*{{Parameter|value}} can be any literal string or number, depending on the value specified in the {{KW|SELECT CASE}} statement.
|
||
|
*Code is executed until the next case, so each case can handle multiple lines of code.
|
||
|
*CASE conditions normally are listed in some logical order going down the page.
|
||
|
*CASE order can affect the SELECT CASE code execution when more than one CASE can be true! This is specially true when multiple conditional operators, CASE IS or TO ranges are used!
|
||
|
* CASE lists can also be listed horizontally by using colon separators between cases.
|
||
|
* 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]]
|
||
|
|
||
|
|
||
|
''Relational Operators:'' Used in {{KW|CASE}} {{KW|IS}} evaluations Only.
|
||
|
|
||
|
* > is Greater than
|
||
|
* < is Less than
|
||
|
* = is Equal to
|
||
|
* >= is Greater than or Equal to
|
||
|
* <= is Less than or Equal to
|
||
|
* <> is Not Equal to
|
||
|
|
||
|
|
||
|
''Multiple Conditional Operators:'' Used in {{KW|CASE}} {{KW|IS}} Only.
|
||
|
|
||
|
* {{KW|AND (boolean)|AND}} can be used to add extra conditions to a {{KW|CASE}} {{KW|IS}} statement evaluation.
|
||
|
* {{KW|OR (boolean)|OR}} can be used to add alternate conditions to a {{KW|CASE}} {{KW|IS}} statement evaluation.
|
||
|
* Parenthesis are allowed in {{KW|CASE}} {{KW|IS}} statements to clarify an evaluation.
|
||
|
* The FIRST time a {{KW|CASE}} value matches the compared variable's value, that {{KW|CASE}} code is executed and {{KW|SELECT CASE}} is exited.
|
||
|
|
||
|
|
||
|
''Example:''
|
||
|
|
||
|
{{CodeStart}}
|
||
|
|
||
|
a = 100
|
||
|
{{Cl|SELECT CASE}} a
|
||
|
{{Cl|CASE}} 1, 3, 5, 7, 9: {{Cl|PRINT}} "Odd values under 10 will be shown."
|
||
|
{{Cl|CASE}} 10: {{Cl|PRINT}} "10 will be shown."
|
||
|
{{Cl|CASE}} 50: {{Cl|PRINT}} "50 will be shown."
|
||
|
{{Cl|CASE}} 100: {{Cl|PRINT}} "This will be shown. (a is 100)"
|
||
|
{{Cl|PRINT}} "(and this)"
|
||
|
{{Cl|CASE}} 150: {{Cl|PRINT}} "150 will be shown."
|
||
|
{{Cl|CASE IS}} < 150: {{Cl|PRINT}} "Less than 150 will be shown. (a which is 100 is under 150)"
|
||
|
{{Cl|CASE}} 50 {{Cl|TO}} 150: {{Cl|PRINT}} "50 to 150 will be shown. (a which is 100 is between 50 TO 150)"
|
||
|
{{Cl|END SELECT}}
|
||
|
|
||
|
{{CodeEnd}}
|
||
|
|
||
|
''Returns:''
|
||
|
|
||
|
{{OutputStart}}
|
||
|
|
||
|
This will be shown. (a is 100)
|
||
|
(and this)
|
||
|
|
||
|
{{OutputEnd}}
|
||
|
: ''Explanation:'' [[SELECT CASE]] compares the variable's value to each descending {{KW|CASE}} until ONE is true, executes the [[CASE]] code and exits the SELECT CASE. [[CASE]] statements should be placed in a increasing or decreasing order for the best results.
|
||
|
|
||
|
: What happens is that since 5 isn't 100 then the code until the next CASE is ignored, the same obviously goes for 10 and 50 but then comes 100 which is what {{Parameter|a}} is so the code in that [[CASE]] is executed.
|
||
|
|
||
|
:* A [[CASE]] can list several values separated by commas for the same program option to be executed.
|
||
|
|
||
|
:* [[CASE IS]] is used when we need to compare the value to a conditional expression range such as a value is "=" equal to, "<" less than, ">" greater than, "<>" not equal to or [[NOT]] a value.
|
||
|
|
||
|
:* A [[CASE]] range can be specified (in the example; 50 {{KW|TO}} 150) if needed.
|
||
|
<center>''Note:'' A {{KW|SELECT CASE}} block has to end with [[END SELECT]].</center>
|
||
|
|
||
|
|
||
|
{{PageSeeAlso}}
|
||
|
* [[CASE ELSE]], [[CASE IS]]
|
||
|
* [[SELECT CASE]], [[END SELECT]]
|
||
|
* [[IF...THEN]]
|
||
|
|
||
|
|
||
|
{{PageNavigation}}
|