# Select statements

Select statements provide an alternative approach to testing conditions. You can use a select statement to examine the value of a variable or expression and act on it in different ways, dependent on what that value is. Here's a simple program that uses the select, case, endcase and endselect keywords to perform a select statement.

    number as integer = 1

    do
    	select number
    		case 1:
    			print ( "1" )
    		endcase

    		case 2:
    			print ( "2" )
    		endcase

    		case 3:
    			print ( "3" )
    		endcase
    	endselect

    	sync ( )
    loop

The program look will display either "1", "2" or "3" on screen dependent on the value of the variable number

The code begins by declaring a variable named number and assigning it a value of 1. This is followed by a do loop where the select statement is implemented.

A select statements operates by looking at the value of a variable or expression and then taking actions based on what that value is. Directly after the select keyword the name of the variable to examine must be supplied. Our program is going to check the value of number, so this is placed after the select keyword.

To act on the variable being checked by the select statement the case keyword is used, followed by a value or expression to check, which if true will allow any code within the case block to be executed. To close a case statement the keyword endcase is used.

Our program has three case statements, handling three possibilities for the number variable. If the value is 1 then the code within the first case statement will be executed. If the value is 2 then the code within the second case statement will be executed. Last of all if the value is 3 then the code within the third and final case statement will run. Try changing the value of number to 2 and then run the program again. Repeat the process by changing its value to 3.

In the event that no case statement deals with the value of the variable then a default case statement can be used. This is achieved by using the default keyword after using case. This program shows how it works.

    data as integer = 99

    do
    	select data
    		case 1:
    			print ( "1" )
    		endcase

    		case 2:
    			print ( "2" )
    		endcase

    		case 3:
    			print ( "3" )
    		endcase

    		case default:
    			print ( "none of the main conditions triggered" )
    		endcase
    	endselect

    	sync ( )
    loop

The select statement in our program looks at the value of data and has code to deal with it being 1, 2 and 3. Given that data is assigned a value of 99 when declared then none of these conditions will be met, so the code within the case default block will be executed.