| expression - the value that each Case will be compared against |
|
Starts a block that picks one branch out of many by comparing a value against a list. Select evaluates its expression once, then runs the first Case that matches it. If none match, the Default branch runs if there is one, and End Select closes the block. Select game_state Case STATE_MENU UpdateMenu() Case STATE_PLAYING UpdateGame() Default RuntimeError "unknown state" End Select That is the classic game state machine, and it is where Select earns its place - dispatching on a state, a menu choice, a message type, a tile value or a key. It says "one of these" far more clearly than a ladder of ElseIf tests. There is no fall-through: once a Case has run, control jumps to End Select. You never need a break statement. Cases must be values to compare against, not conditions - but there is a well-loved trick around that. Select True, then write full conditions in each Case, and each is compared against True. That gives you ranges and multi-variable tests inside a Select: Case score > 100. See also: Case, Default, End Select, If, True. |
; Select Example
; --------------
; Inspect each weapon slot and see which branch Select picks
For slot=1 To 4
Print "Weapon slot "+slot+":"
; Select compares its value against each Case in turn
Select slot
Case 1
Print " Laser - rapid fire, low damage"
Case 2
Print " Missile - slow but devastating"
Case 3
Print " Flamethrower - short range, big burn"
Default
Print " Empty - nothing equipped"
End Select
Next
Print ""
Print "Press any key to close the example"
WaitKey
End