Name:
select
Syntax:

SELECT CASE variable
  CASE value
     {code}
  CASE value, value...
     {code}
  CASE value TO value
     {code}
  CASE comparison value
     {code}
  ELSE

     {code}
ENDSELECT

Variable - is the value to test.

Value - is a variable or constant to test againt

Comparison - can be any of the following conditions

= equal to
is equal to
<> not equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
Description:

Conditionally execute sections of code depending on a variable's value.

Multiple tests can check for various conditions and if those conditions are met then the associated code is executed and then execution continues after the endselect command. Only one condition will ever be met within and 'select case' command and oly one section of associated code will be executed.

If a condition test is not met then program execution continues with the next case command. If that condition is met then the associated code is executed and then execution continues after the endselect command.

If no conditions have been met and an 'else' command is encountered then its associated code will be executed. The code associated with the 'else' condition is only executed when no other condition has been met.

The 'select case' command may often be used in preference to nested 'if' commands or sequences of 'if' and 'elseif' commands to make code execution clearer or the source code more readable, particularly that with 'select case' the variable being tested only needs to be specified once. The choice of which to use is mainly a matter of personal preference and favoured programming style.

 

SELECT CASE variable

Specifies the variable whose value will be tested in subsequent 'case' commands.

 

CASE value

If the 'select case' variable matches the value then the associated code will be executed. Then code execution continues after the endselect command.


CASE value, value...

Additional values to be checked for by adding them as a comma seperated list after the 'case' command. If the 'select case' variable matches any of the values then the associated code will be executed. Then code execution continues after the endselect command.

 

CASE value TO value

If the 'select case' variable has a value of or between the two values specified then the associated code will be executed. Then code execution continues after the 'endselect' command. Note that the left-most value must be less than or equal to the right-most value.

 

CASE comparison value 

The 'select case' variable is compared to the value specified using the comparison operator specified and if the comparison evaluates as true the associated code will be executed. Then code execution continues after the endselect command. The comparison operator can be any of the following conditions -

= equal to
is equal to
<> not equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to


ELSE

If no conditions have been met then the code associated with the 'else' command will be executed. The code associated with the 'else' condition is only executed when no other condition has been met so it is a 'catch-all' for all other 'select case' variable values which do not match any explicit 'case' conditions.

 

ENDSELECT

Terminates the 'select case' command. The command can either be specified as a single word, 'endselect' or as two separate words, 'end select'.

Applies To:
All
See Also:
Related Create:
    Share:
    Print:

    Switch outputs depending on the value of a variable

    Switch outputs 1 and 2 on or off depending on the value of variable b1

    Code Example:
    	select case b1
    	  case 1		; if b1 is 1, turn output 1 on
    	    high 1
    	  case 2,3		; if b1 is 2 or 3, turn output 1 off
    	    low 1
    	  case 4 to 6		; if b1 is between 4 and 6 (inclusive), turn output 2 on
    	    high 2
    	  else			; if none of these are true, turn output 2 off
    	    low 2
    	endselect
    Copy Code Submit an Example

    Submit Your Own Code!

    You must be logged in to submit code examples. Login now.