The select branch jumps to the literal that is identical to the result of the expression. The label of the case branch must BE literal ( EG: 1, 2, "3", "Cheese" or a constant: [#CONSTANT ONE = 1] ).
Select 1 = 1, result is 1, so for each case, if case is 1, select case.
An if statement does not need to jump anywhere other than the line below or after the THEN keyword.
If 1 = 1 Then DoThis
In other words; ask the question: If N is 10, is N lower than 3, lower than 6 and lower than 12. How could you write this easily in a select branch?
Select n < 10
Case n < 3
EndCase
Case n < 6
EndCase
Case n < 12
EndCase
EndSelect
The above code will not work.
Although it is possible to do something like this:
n = 25
Select n < 12
Case 1
print "Less than 12"
Endcase
Case Default
print "more or equal to 12"
Endcase
Endselect
It is recommended to use the following select statement over a series of if statements any day, it would run much faster.
rem Pick a random function, or a default function if 0, 4 or 5 is returned
#CONSTANT ONE 1
Select Rnd(5)
Case ONE
DoAction1()
EndCase
Case 2
DoAction2()
EndCase
Case 3
DoAction3()
EndCase
Case Default
DoDefaultAction()
EndCase
EndSelect
rem Select the name of the action and perform the action
Input "Enter command: ", action$
Select Lower$( action$ )
Case "move"
Move Object id, 10
EndCase
Case "turnleft"
Turn Object Left id, 90
EndCase
Case "turnright"
Turn Object Right id, 90
EndCase
Case "digfortreasure"
DigForTreasure()
EndCase
EndSelect
If the previous results needed to be changed by the user, the if statement should be used:
rem Pick a random function, or a default function if the variable options are not returned
FirstOption = 4
SecondOption = 2
ThirdOption = 0
option = Rnd(5)
If option =
DoAction1()
Else
If option = SecondOption
DoAction2()
Else
If option = ThirdOption
DoAction3()
Else
DoDefaultAction()
EndIf
EndIf
EndIf
rem Select the name of the action and perform the action
Input "Enter command: ", action$
action$ = Lower$( action$ )
If action$ = moveAction$
Move Object id, 10
EndIf
If action$ = moveAction$
Turn Object Left id, 90
EndIf
rem And so on
Another handy use for select branches is to pick an array, or pointer to a variable, or a pointer to a function
Rem Requires Matrix1 utilities
Dim Array1$(1)
Dim Array2$(1)
Dim SelectedArray$()
Array1$(1) = "Tom"
Array2$(1) = "Harry"
n$ = ""
While n$ = ""
Input "Select array name? "; n$
Endwhile
Select Val( n$ )
Case "Array1"
Link Array SelectedArray$(), Get ArrayPtr( Array1$() )
Endcase
Case "Array2"
Link Array SelectedArray$(), Get ArrayPtr( Array2$() )
Endcase
Endselect
Print "This array contains "; SelectedArray$(1)
Wait key