The format of a Select...EndSelect block is shown below:
Select <expression>
Case <constant 1>
; Instructions
[Case <constant 2>
; Instructions]
[Case <constant 3>
; Instructions]
; ...
[Case <constant n>
; Instructions]
[Default
; Instructions]
EndSelect
The operation of this structure is as follows. First of all the value of the expression is
calculated. This can be any kind of expression, not just a comparison or logical one. It is
then compared to each of the constants (numeric or string) which follow the Case keywords.
If it matches any of those then the code following the matching Case is executed, until another
Case keyword is reached, at which point the program continues from the next instruction
after the EndSelect keyword. If none of the Case conditions match then the instructions
after the Default keyword are executed.
Again, the square brackets show that these conditions are all optional, although you must have at least one Case condition, and the Default keyword (if present) must be the last condition within the Select...EndSelect block.
Just as with the If...EndIf blocks, you can nest Select...EndSelect blocks inside each other or inside any other blocks or loops as long as one block is entirely contained within the other.
The example below shows some uses of the Select...EndSelect block. You can also download the sources from here.
If OpenConsole()
; This is the same as one of the previous examples, although
; the comparisons are always for equality so we cannot do the
; same as the ElseIf name$<>"John" part of the previous example
name$ = "John"
Select name$
Case "Andrew"
PrintN("You have the same name as my brother.")
Case "Robert"
PrintN("Were you named after the famous Scottish king?")
Case "John"
PrintN("Where can we find Paul, George and Ringo?")
Default
PrintN("Sorry, I do not recognise you.")
EndSelect
; A simple set of instructions for farmers, depending on the age of their chickens ;)
; (NB: no animals were harmed during the testing of this example!)
chicken_age.w = -1
Select chicken_age
Case 0
PrintN("Just a chick more than a chicken")
Case 1
PrintN("Useful for laying eggs")
Case 2
PrintN("Prime age for meat - choppity chop chop! >)")
Case 3
PrintN("Getting old now, better put it out of its misery - choppity chop chop! >)")
Default
PrintN("Counting them before they are hatched?")
EndSelect
PrintN("Press return to exit program.")
Input()
CloseConsole()
EndIf
End
This example also gives a better idea of how to nest different types of blocks or loops. You will see that the order of start and end keywords are If, Select, EndSelect, Select, EndSelect and EndIf. This is the correct way to nest them, with the Select...EndSelect blocks completely enclosed within the If...EndIf block. It would be an error to have the program written in such a way that the order was (for example) If, Select, EndSelect, Select, EndIf and EndSelect.
| Previous topic | Chapter contents | Next topic |
|---|---|---|
| Choosing to execute some instructions | User Guide contents | Repeating instructions a number of times |