IB Statements/select
From CometWiki
(New page: '''SELECT statement''' '''Syntax:''' SELECT CASE test expression CASE expression list 1 [statement block 1] CASE expression list 2 [statement block 2] . . . ...) |
|||
(2 intermediate revisions not shown) | |||
Line 5: | Line 5: | ||
CASE expression list 1 | CASE expression list 1 | ||
[statement block 1] | [statement block 1] | ||
- | |||
CASE expression list 2 | CASE expression list 2 | ||
[statement block 2] | [statement block 2] | ||
Line 13: | Line 12: | ||
CASE expression list n | CASE expression list n | ||
[statement block n] | [statement block n] | ||
- | |||
[CASE ANY MATCH] | [CASE ANY MATCH] | ||
[statement block] | [statement block] | ||
- | |||
[CASE ELSE] | [CASE ELSE] | ||
[statement block] | [statement block] | ||
- | |||
ENDSELECT | ENDSELECT | ||
Line 35: | Line 31: | ||
IS relational-operator expression | IS relational-operator expression | ||
The expressions and operators in the expression list must be compatible with the test expression. The word "IS" must precede any relational operators. The word "FROM" must precede any range specification. The word "TO" means up to but not including. The word "THRU" means up to and including. | The expressions and operators in the expression list must be compatible with the test expression. The word "IS" must precede any relational operators. The word "FROM" must precede any range specification. The word "TO" means up to but not including. The word "THRU" means up to and including. | ||
- | The selected expression is tested in sequence for each of the CASE clauses. Upon reaching the first successful test, the statement block following the CASE is executed, and control is then passed to one of | + | The selected expression is tested in sequence for each of the CASE clauses. Upon reaching the first successful test, the statement block following the CASE is executed, and control is then passed to one of two places: |
- | If the structure does not contain a CASE ANY MATCH clause | + | If the structure does not contain a CASE ANY MATCH clause control is passed to the statement following the ENDSELECT statement. |
- | If the structure contains a CASE ANY MATCH clause, control is passed to it. Following the execution of the CASE ANY MATCH statement block, control is passed to | + | If the structure contains a CASE ANY MATCH clause, control is passed to it. Following the execution of the CASE ANY MATCH statement block, control is passed to the statement following the ENDSELECT statement. |
- | If the structure | + | If no match is found and the structure contains a CASE ELSE clause, control is passed to it. The CASE ELSE statement block is executed and control is passed to the statement following the ENDSELECT statement. |
- | + | A BREAK statement may be used in the CASE structure. If there is a CASE ANY MATCH clause, control is passed to it. If not the BREAK statement transfers control to the statement following the ENDSELECT statement. | |
- | + | ||
- | + | ||
- | + | ||
'''Example:''' | '''Example:''' | ||
- | SELECT CASE TEMP ! use the TEMP variable | + | SELECT CASE TEMP ! use the TEMP variable |
- | + | ! | |
- | CASE IS GT 102 ! a relational operator clause | + | CASE IS GT 102 ! a relational operator clause |
- | PRINT (0) "It's over 102 degrees" ! conditional PRINT | + | PRINT (0) "It's over 102 degrees" ! conditional PRINT ! |
- | + | ! | |
- | CASE 100;101;102 ! multiple expressions | + | CASE 100;101;102 ! multiple expressions |
- | PRINT (0) "It's a bit above 100" ! conditional PRINT | + | PRINT (0) "It's a bit above 100" ! conditional PRINT |
- | + | ! | |
- | IF TEMP LT 102 BREAK ! if true, jump to end | + | IF TEMP LT 102 BREAK ! if true, jump to end |
PRINT (0) " and heading higher" ! otherwise, PRINT | PRINT (0) " and heading higher" ! otherwise, PRINT | ||
- | ENDIF ! end of IF structure | + | ENDIF ! end of IF structure |
- | + | ! | |
- | CASE FROM 80 THRU 99 ! range of values | + | CASE FROM 80 THRU 99 ! range of values |
- | PRINT (0) "It is mighty warm today" ! conditional PRINT | + | PRINT (0) "It is mighty warm today" ! conditional PRINT |
- | + | ! | |
- | CASE ANY MATCH ! if any of above match | + | CASE ANY MATCH ! if any of above match |
- | PRINT (0) "Hope is it warm tomorrow" ! conditional PRINT | + | PRINT (0) "Hope is it warm tomorrow" ! conditional PRINT |
- | + | ! | |
- | CASE ELSE ! if none of above match | + | CASE ELSE ! if none of above match |
- | PRINT (0) "It is below 80 degrees" ! conditional PRINT | + | PRINT (0) "It is below 80 degrees" ! conditional PRINT |
- | + | ! | |
- | ENDSELECT ! end of CASE structure | + | ENDSELECT ! end of CASE structure |
Line 77: | Line 70: | ||
SELECT CASE test expression ! identify the test expression | SELECT CASE test expression ! identify the test expression | ||
- | + | ! | |
CASE expression list 1 ! test the first case | CASE expression list 1 ! test the first case | ||
[statement block 1] ! execute if first case is valid | [statement block 1] ! execute if first case is valid | ||
- | + | ! | |
CASE expression list 2 ! test the second case | CASE expression list 2 ! test the second case | ||
[statement block 2] ! execute if second case is valid | [statement block 2] ! execute if second case is valid | ||
Line 88: | Line 81: | ||
CASE expression list n ! test the nth case | CASE expression list n ! test the nth case | ||
[statement block n] ! execute if nth case is valid | [statement block n] ! execute if nth case is valid | ||
- | + | ! | |
CASE ANY MATCH ! if any of the above cases are | CASE ANY MATCH ! if any of the above cases are | ||
[statement block] ! matched, execute this block of code | [statement block] ! matched, execute this block of code | ||
! and jump to end of structure | ! and jump to end of structure | ||
! (this is an optional clause) | ! (this is an optional clause) | ||
- | + | ! | |
CASE ELSE ! otherwise, | CASE ELSE ! otherwise, | ||
[statement block] ! execute this block of code | [statement block] ! execute this block of code | ||
! (this is an optional clause) | ! (this is an optional clause) | ||
- | + | ! | |
ENDSELECT ! end the structure | ENDSELECT ! end the structure | ||
Line 167: | Line 160: | ||
ENDSELECT | ENDSELECT | ||
- | The CASE structure may also include a BREAK statement. A BREAK statement | + | The CASE structure may also include a BREAK statement. A BREAK statement transfers control to the CASE ANY MATCH (if there is one) otherwise to the statement following the ENDSELECT statement. For example: |
SELECT CASE VALUE ! test the numeric variable VALUE | SELECT CASE VALUE ! test the numeric variable VALUE |
Latest revision as of 19:49, 1 July 2015
SELECT statement
Syntax: SELECT CASE test expression
CASE expression list 1 [statement block 1] CASE expression list 2 [statement block 2] . . . CASE expression list n [statement block n] [CASE ANY MATCH] [statement block] [CASE ELSE] [statement block] ENDSELECT
Discussion: Executes none or one of the several statement blocks, depending on the value of the test expression.
The statement blocks do not need to be indented. They are shown that way for the sake of clarity and source code readability.
The test expression may be any numeric or string expression.
The expression list elements may have any of the following forms:
expression or expression list (expression list elements are separated with a semicolon) FROM expression TO expression FROM expression THRU expression IS relational-operator expression The expressions and operators in the expression list must be compatible with the test expression. The word "IS" must precede any relational operators. The word "FROM" must precede any range specification. The word "TO" means up to but not including. The word "THRU" means up to and including. The selected expression is tested in sequence for each of the CASE clauses. Upon reaching the first successful test, the statement block following the CASE is executed, and control is then passed to one of two places:
If the structure does not contain a CASE ANY MATCH clause control is passed to the statement following the ENDSELECT statement.
If the structure contains a CASE ANY MATCH clause, control is passed to it. Following the execution of the CASE ANY MATCH statement block, control is passed to the statement following the ENDSELECT statement.
If no match is found and the structure contains a CASE ELSE clause, control is passed to it. The CASE ELSE statement block is executed and control is passed to the statement following the ENDSELECT statement.
A BREAK statement may be used in the CASE structure. If there is a CASE ANY MATCH clause, control is passed to it. If not the BREAK statement transfers control to the statement following the ENDSELECT statement.
Example:
SELECT CASE TEMP ! use the TEMP variable ! CASE IS GT 102 ! a relational operator clause PRINT (0) "It's over 102 degrees" ! conditional PRINT ! ! CASE 100;101;102 ! multiple expressions PRINT (0) "It's a bit above 100" ! conditional PRINT ! IF TEMP LT 102 BREAK ! if true, jump to end PRINT (0) " and heading higher" ! otherwise, PRINT ENDIF ! end of IF structure ! CASE FROM 80 THRU 99 ! range of values PRINT (0) "It is mighty warm today" ! conditional PRINT ! CASE ANY MATCH ! if any of above match PRINT (0) "Hope is it warm tomorrow" ! conditional PRINT ! CASE ELSE ! if none of above match PRINT (0) "It is below 80 degrees" ! conditional PRINT ! ENDSELECT ! end of CASE structure
Additional discussion: The SELECT structure encompasses several Internet Basic keywords, including SELECT, CASE, ENDSELECT, FROM, TO, THRU, IS, ANY, and MATCH. The basic outline of a CASE structure is:
SELECT CASE test expression ! identify the test expression ! CASE expression list 1 ! test the first case [statement block 1] ! execute if first case is valid ! CASE expression list 2 ! test the second case [statement block 2] ! execute if second case is valid . . . CASE expression list n ! test the nth case [statement block n] ! execute if nth case is valid ! CASE ANY MATCH ! if any of the above cases are [statement block] ! matched, execute this block of code ! and jump to end of structure ! (this is an optional clause) ! CASE ELSE ! otherwise, [statement block] ! execute this block of code ! (this is an optional clause) ! ENDSELECT ! end the structure
The test expression in the SELECT CASE statement may be any numeric or string expression.
The expression list elements in the CASE statement may have any of several forms. The first form is simply an expression or constant value. For example:
SELECT CASE VALUE ! test the numeric variable VALUE CASE 50 ! if VALUE = 50, [statement block] ! execute this code CASE 75 ! if VALUE = 75, [statement block] ! execute this code ENDSELECT ! end the structure
Multiple expression list elements may be included in a single CASE statement. The elements are separated with a semicolon. For example:
SELECT CASE VALUE ! test the numeric variable VALUE CASE 50;51;52;53 ! if VALUE is 50, 51, 52 or 53, [statement block] ! execute this code ENDSELECT ! end the structure
The CASE statement may also indicate a range of values, as follows:
CASE expression TO expression ! up to but not including CASE FROM expression THRU expression ! up to and including
An example of this is:
SELECT CASE VALUE ! test the numeric variable VALUE CASE 50 TO 60 ! if VALUE is 50 to (but not ! including) 60, [statement block] ! then execute this code ! CASE FROM 80 THRU 90 ! if VALUE is 80 to (and including) [statement block] ! 90, then execute this code ! ENDSELECT ! end the structure
The CASE statement may also include a relational-operator clause, as follows:
CASE IS relational-operator expression
An example of this is:
SELECT CASE VALUE ! test the numeric variable VALUE CASE IS GT 102 ! if VALUE is greater than 102, [statement block] ! then execute this code ENDSELECT ! end the structure
The CASE ANY MATCH clause is executed if one of the specific CASE values is matched. This statement is optional, but if included in the structure, must be written after the individual CASE clauses and before the CASE ELSE clause (if one is present). For example:
SELECT CASE VALUE ! test the numeric variable VALUE CASE 50 ! if VALUE = 50, [statement block] ! execute this code CASE 75 ! if VALUE = 75, [statement block] ! execute this code CASE ANY MATCH ! if either of above cases is [statement block] ! matched,execute this code, too ENDSELECT ! end the structure
The CASE ELSE clause may be included in the structure to handle cases that are not matched by any of the individual CASE clauses. This clause is optional, but if included in the structure, must be written as the final clause (just prior to the ENDSELECT statement). For example:
SELECT CASE VALUE ! test the numeric variable VALUE . . . CASE ELSE ! if none of the cases produce a [statement block] ! match, execute this code ENDSELECT
The CASE structure may also include a BREAK statement. A BREAK statement transfers control to the CASE ANY MATCH (if there is one) otherwise to the statement following the ENDSELECT statement. For example:
SELECT CASE VALUE ! test the numeric variable VALUE . CASE ________ ! test one of the specific cases . IF ________ THEN BREAK ! if a condition is true, then break . ENDSELECT