Remembering Logo
Turtle graphics tutorial
Scripting language tutorial
Changing window icons at runtime
A real spinner control
Tip Corner - Select Case
Hocus Focus
Illuminated Radiobuttons
Select Case was used by the parsing routine in the scripting language tutorial:
Select Case c$ Case "center" 'equivalent to LB home command Case "north" 'equivalent to LB north command Case "south" 'south=north+180 Case "east" 'east=north+90 Case "west" 'west=north+270 Case "turn" 'equivalent to LB turn command Case "moveto" 'goto point indicated Case "move" 'travel set distance in current direction Case "down" 'equivalent to LB down command Case "up" 'equivalent to LB up command Case Else 'script command not understood by parser End Select
The following primer on Select Case is from the ebook, Mastering Liberty
BASIC 3, available here:
http://iquizme.0catch.com/lb/lbw3/master.html
***********
Select Case from Mastering Liberty BASIC 3
(All Rights Reserved)
A program can evaluate a set of conditions with the IF...THEN...ELSE construction. If the set of conditions evaluates to TRUE, the code after THEN is executed. If there is an ELSE, then it is executed if the set of conditions evaluates to FALSE. To evaluate a complex set of conditions with IF...THEN...ELSE requires numerous similar statements. This can quickly get cumbersome and difficult to modify and debug.
Liberty BASIC 3 brings a new construction for evaluating and acting on sets of conditions, called SELECT CASE. The syntax for Select Case is:
SELECT CASE var CASE x 'basic code 'goes here CASE y 'basic code 'goes here CASE z 'basic code 'goes here CASE else 'basic code 'goes here END SELECT
The first line in a select case statement is "select case" followed by the named variable or the expression that will be selected. On subsequent lines, there are "case" statements, specifying the conditions to evaluate for the selected variable, with code after the "case" statement that is to be executed if that particular case evaluates to TRUE. The "select case" routine must end with the words "end select".
There is no limit to the number of conditions that can be used for evaluation. The program can also specify a routine to perform if none of the conditions is met by using the "case else" designation.
Here is a trivial example. The variable selected is called "num" so the statement reads "select case num". If the value of "num" equals "1" then the program will print "one", as determined by the "case 1" statement and the code which follows it. The same is true for "case 2" and "case 3". If the value of "num" is not specified in one of the case statements, then the code which follows "case else" is executed.
num = 3
select case num case 1 print "one" case 2 print "two" case 3 print "three" case else print "other number" end select
The example above results in output in the mainwindow of:
three
The example above had a single variable in the "select case" statement. It is also permissable to use an expression in the "select case" statement. In the following example, (num * a) is to be evaluated.
num = 3 a = 2
select case num * a case 1 print "one" case 2 print "two" case 3 print "three" case else print "other number" end select
The example above results in output in the mainwindow of:
other number
STRINGS
SELECT CASE also works for string variables.
animal$ = "cat"
select case animal$ case "dog" print "woof" case "cat" print "meow" case "bird" print "tweet" case else print "other animal" end select
The example above results in output in the mainwindow of:
meow
MULTIPLE CONDITIONS
IF...THEN...ELSE can evaluate multiple condions at once, and this is also
true for SELECT CASE. For simple expressions, separate the expressions with
commas. In the example below, there is a case for num to equal either 1, 2,
or 3.
num = 3
select case num
case 1,2,3 print "less than four" case 4 print "four" case 5 print "five" case else print "other number" end select
The printout for the example above is
less than four
Notice that once a CASE is met, later CASE statements are ignored and program execution continues at the line after the END SELECT statement. In the following example, even though all three cases are met, only the first one is used. Once the first CASE statement is met, program execution jumps out of the SELECT CASE... END SELECT routine.
num = 3
select case num case 1,2,3 print "less than four" case 2,3,4 print "more than one" case 1,3,5 print "odd number" end select print "All done."
The printout for the example above is
less than four
All done.
MULTIPLE EXPRESSIONS
There is a way to evaluate multiple conditions that contain boolean expressions.
It can be done by using SELECT CASE without an expression. Omitting the expression
in the select case statement causes the conditions to be evaluated in their
entirety. To omit the expression, type "select case" with no words
following it on the line of code.
value = 109 print "value = "; value print select case case value < 10 or (value > 100 and value < 110) print "value < 10 or (value > 100 and value < 110)" case value > 200 and value < 210 print "(value > 200 and value < 210)" case value = 110 or value = 112 print "value = 110 or value = 112" end select
MULTIPLE STRINGS
String variables can also be evaluated for multiple conditions in a single
CASE statement by separating them with commas, just as it is done for numeric
expressions. This is a trivial example to show multiple string CASE evaluations:
var$="hello"
select case var$ case "howdy" print "Howdy to you, too." case "hi","hello" print "Good day." case else print "See you later." end select
EXITING SELECT CASE
The code to be executed for any of the specified cases may be placed directly
after the CASE statement. It is also correct to have a "goto [branchLabel"
following a CASE statement. The code may also include a GOSUB, a SUB or a
FUNCTION. If the program is not sent to another location to continue execution,
then the code after the END CASE statement will be carried out once any specified
code within the CASE statements is executed.
animal$ = "bird"
select case animal$ case "dog" call Woof case "cat" goto [cat] case "bird" print DoTweet$() case else print "other animal" end select
'other basic code goes here wait
[cat] print "meow" wait
sub Woof print "woof" end sub
function DoTweet$() DoTweet$ = "tweet" end function