Select-Case Statement

Top  Previous  Next

 

Function:

A way to conditionally execute code.

Syntax:

 

select select_expression

       case expression1_1 [ , expression1_2, … ] [ : ]

               statement1_1

               statement1_2

               

       case expression2_1 [ , expression2_2, … ] [ : ]

               statement2_1

               statement2_2

               

       

       [ case else [ : ]

               statementN_1

               statementN_2

               

       ]

end select

Scope:

Local and HTML

See Also:

If.... Then... Else Statement

 


Part

Description

select_expression

Required. The expression which is evaluated first; subsequent expressions are tested to match this expression. If a match is found, the statements contained within this case clause are executed, and execution then resumes from the line immediately following end select.

expression[1_1... N_1]

Required. An expression to evaluate; If it matches the select_expression, the statements included in this case clause are executed.

statement[1_1... N_1]

Required. Statements to execute when expression[1_1... N_1] matches select_expression.

:

Optional. Maintained for backwards compatibility -- some versions of BASIC in the past required a colon following every case expression.

case else

Optional. Precedes a block statements which is executed if neither of the earlier case clause match the select_expression. If present, must be the last case clause.

Details

It is of note that once a matching case clause is found, no other case clauses are tested; the code within the matching clause is simply executed, and execution resumes from the line following end select.

Also, remember that writing two select_expressions at the same time does not mean that there will be a shared code for both of them. Rather, it means that the first expression will have no code associated with it!

 

 

...

case 1 : 'this expression has no code associated with it

case 2 : 'code 'x=5' belongs to this expression

  x=5

...

 

 

Correct way to have a single block of code for two expressions is as follows:

 

 

...

case 1,2 : 'x=5 will be done both for 1 and 2

  x=5

...

 

Examples

 

sub print_weekday (weekday as byte)

       select case weekday

               case 1 : ser.setdata("Monday")

               case 2 : ser.setdata("Tuesday")

               case 3 : ser.setdata("Wednesday")

               case 4 : ser.setdata("Thursday")

               case 5 : ser.setdata("Friday")

               case 6 : ser.setdata("Saturday")

               case 7 : ser.setdata("Sunday")

               case else : ser.setdata("Did you just invent a new day?")

       end select

       ser.send

end sub