IB Statements/select

From CometWiki

< IB Statements
Revision as of 11:35, 20 May 2009 by Badge (Talk | contribs)
Jump to: navigation, search

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 several places:

If the structure does not contain a CASE ANY MATCH clause or a CASE ELSE clause (both clauses are optional), 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 one of two places:

If the structure does not contain a CASE ELSE clause, control is passed to the statement following the ENDSELECT statement.

If 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.

If the structure contains a CASE ELSE and does not contain a CASE ANY MATCH clause, control is passed to the CASE ELSE clause. 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 used, the BREAK statement must be imbedded within an IF/THEN or IF/THEN/ELSE structure. 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, which must be imbedded within an IF/THEN or IF/THEN/ELSE structure, transfers control 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
Personal tools