Select / Case statements are an alternative to If / Then statements when you have a large number of conditions to examine.
The simplest form of a Select/Case block looks like this
myInt = 1 ; myInt = 2 ; myInt = 3 Select myInt Case 1 Print "The first case" Case 2 Print "The second case" EndSelect Sync WaitKey |
If the variable myInt equals 1 this example will output The first case if myInt equals 2, it will output The second case. All other values of myInt will cause no output at all. In order to process all cases that are not explicitely stated with the Case keyword, you can use the keyword Default:
; myInt = 1 ; myInt = 2 myInt = 3 Select myInt Case 1 Print "The first case" Case 2 Print "The second case" Default Print "myInt is neither 1 nor 2" EndSelect Sync WaitKey |
This example would output myInt is neither 1 nor 2 if the value of myInt is less than 1 or greater than 2.
You can also check for value ranges with in the case statements by using the keyword To. myInt = 1 ; myInt = 42 Select myInt Case 1 To 2 Print "myInt is either 1 or 2" Case 10 To 100 Print "myInt is greater than 9" Print "myInt is smaller than 101" Default Print "The value myInt is neither 1 nor 2" Print "nor between 10 or 100" EndSelect Sync WaitKey |
If certain conditions apply to a number of unrelated values, you can list these values after the case keyword, separated by commas:
myInt = 42 Select myInt Case 14, 42, 78 Print "It's either 14 42 or 78" Case 1 To 10 Print "It's between 1 and 10" Default Print "Neither of the above" EndSelect Sync WaitKey |
By default PlayBASIC will exit a Select/ EndSelect block as soon as a case match occurs. In order to force PlayBASIC to check further cases, use the ContCase statement:
; myInt = 14 myInt = 42 ; myInt = 178 Select myInt Case 14, 42, 178 Print "It's either 14, 42 or 178" ContCase Case 1 To 100 Print "It's between 1 and 100" EndSelect Sync WaitKey |
Select / Case statements work with all standard types, ie. Integer, Float & String. Opposed to a lot of other Basic dialects, PlayBasic also accepts expressions after the Case keyword.
The expression followed by the Select keyword must be of the same type as the expressions or constants followed by the Case statements. Otherwise PlayBasic will return an error.
|
Example Source: Download This Example Global test1=10 Global test2=20 a=1 a#=1.5 Test_Integer_select(a) Test_Float_select(a#) Test_String_select("sat") Sync WaitKey End Function test_integer_select(a) Print "Select VALUE:"+Str$(a) Select a Case 1 Print "yeah i'm the first option" Case 2 Print "yeah i'm second option" Case 3 Print "yeah i'm third option" Case 10 To 100 Print "10 Range" Case Test1+test2 Print "Matched the Test Cases" Default Print "didn't match anything" EndSelect Print "" EndFunction Function test_Float_select(a#) Print "Floating Select VALUE:"+Str$(a#) Select a# Case 5.7,5.8,5.9 Print "yeah i'm in th list of options" Case 1.5 Print "yeah i'm the first option" Case 1.5 Print " i'm the first option AGAIN ?" Case 2.5 Print "yeah i'm second option" Case 3.5 Print "yeah i'm third option" Case 7.6 To 66.5 Print "with the Range 7.5 To 66.5" Case Test1+test2 Print "Matched the Test Cases" Default Print "didn't match anything" EndSelect Print "" EndFunction Function test_String_Select(a$) Print "String Select String: "+a$ Select a$ Case "hello" Print "im option one" Case "world" Print "im option two" Case "a" To "g" Print "im from A-G" Case "mon","tue","wed","thurs","fri" Print "Days of the week I don't like" Case "sun","sat" Print "The two best days of the week, Sat/Sun" Default Print "didn't match any options" EndSelect Print "" EndFunction |
|