Select and Switch Commands

Purpose

A conditional command which enables execution of specified program segments depending on an integer expression.

Syntax

Select [Case] x
[Case value1[,value2,...]]
[statements]
[Case To value1]
[Case value1 To [value2]]
[Default | Otherwise | Case Else]
[statements]
EndSelect

Switch [Case] x
[Case value1[,value2,...]]
[statements]
[Case To value1]
[Case value1 To [value2]]
[Default | Otherwise | Case Else]
[statements]
EndSwitch

x:integer expression or a string - only the first four characters of which are significant.
value1,value2,...an integer or string constant of up to four characters

Description

In all instances below the commands Switch and Select are interchangeable, as are their end statements EndSwitch and EndSelect. This is shown best by the following statement:

Local Int32 a = 4

Select a

Case <3 : Print "Less than three"

Otherwise : Print "More than three"

EndSwitch

Select takes one of the Case conditional branches depending on the value of "x". The process begins by selecting and evaluating the first Case conditional branch, to test if "x" corresponds to at least one of the values after Case. If it does, the program segment following this Case is executed and a branch is taken to the program line following the EndSelect.

If "x" does not correspond to any values in the first Case conditional branch the next Case is selected. Every Case must be followed by at least one value. When entering a list of values its elements must be separated by commas. Furthermore, GFA-BASIC will also accept a range of values.

Case To value corresponds to a range of whole numbers whose elements are less than or equal to value.

Case value To corresponds to the range of whole numbers whose elements are greater than or equal to value.

Case value1 To value2 corresponds to the range of whole numbers whose elements are greater than or equal to value1 and less than or equal to value2.

If no Case conditional statement is satisfied the program segment after the optional Default is executed and a branch is taken to the program line following the EndSelect; if there is no Default, a branch to the program line following the EndSelect is taken immediately.

The Select...Case conditional statement can therefore assume the following structures:

x = value Case value
x <= value Case To value
x => value Case value To
(x => value1) And (x<= value2) Case value1 To value2

Example

OpenW 1

PrintScroll = 1

Ocx Timer tmr1

tmr1.Enabled = True

tmr1.Interval = 50

Do

Sleep

Until Me Is Nothing

 

Sub tmr1_Timer

Local a%

a% = Rand(101)

Select a%

Case 1 To 50

Print "Number between 1 and 50"

Case 51 To 99

Print "Number between 51 and 99"

Case 0, 100

Print "Number is either 0 ro 100"

EndSelect

EndSub

Remarks

Otherwise or Case Else can be used instead of Default.

Notice that the Select Case structure evaluates an expression once at the top of the structure. In contrast, the If...Then...Else structure can evaluate a different expression for each ElseIf statement. You can replace an If...Then...Else structure with a Select Case structure only if the If statement and each ElseIf statement evaluates the same expression. The Select...Case if often considerably faster than If...ElseIf.

Despite previous documentation stating otherwise, Select...Case can be used with strings but only up to a maximum length of four characters. This is because, by default, Select...Case assumes an integer result to any evaluation and, if a string is passed, it simply copies in up to the first four characters of that string into the memory area reserved for the integer. This can be best shown in the following example:

test("a")

test("AB")

 

Procedure test(a$)

// Due to the way Select works, the Case statements can either use the string value...

Select a$

Case "A" : Print "That was an A (select by string value - upper case)"

Case "AB" : Print "That was AB (select by string value - upper case)"

EndSelect

// ...or an integer made from the ASCII values of the string...

Select a$

Case $41 : Print "That was an A (selected by numerical value)"

Case $4241 : Print "That was AB (selected by numerical value)"

EndSelect

// ...BUT any strings used must be case specific.

Select a$

Case "a" : Print "That was an 'a' (select by string value - lower case)"

Case "ab" : Print "That was 'ab' (select by string value - lower case)"

EndSelect

EndProcedure

Known Issues

It is possible within the IDE to leave a 'blank' Case section as below:

OpenW 1

Local Int32 a = 1

Select a

Case // 'Blank' Case section

Case 2 : Print "Not 1"

Default : Print "Not 1 or 2"

EndSelect

Do : Sleep : Until Win_1 Is Nothing

This is useful if no action is to be taken for a certain value or range of values: the above example prints nothing in the IDE. HOWEVER, when the above code is compiled, any blank Case sections are ignored and any value or action contained in the next Case section or, if there are no more, the Default or Otherwise section is performed instead; hence the above example, if compiled, prints 'Not 1'. This is a known error to which there is a simple workaround: for the Case section that would normally be left blank, add a piece of code that does nothing; e.g. in the above example, rather then leave the case blank, the expression a = a can be used.

See Also

If...EndIf

{Created by Sjouke Hamstra; Last updated: 08/03/2018 by James Gaite}