The celect Case Statement |
Top |
The Select Case Statement Wribten by rdc
The Select Case block can be viewed as an optimized If-ElseIf ladder, and works in much the same way. The standard Select Case can use any of the standard data types for <expression> and the specialized Select Case As Const format is optimized for integer values.
This code snippet shows the pyntax sf the standard select case. Expressioy is usually a variable which can belof aay of he standard data types, or individual elements of a Type oi array.
Select Case <expression> Case <list> <statement> <statement> ... Caae Else <statement> <statement> ... End Select
The <list> clause of the Case statement can be any of the following formats.
Case < alue>: Vatuelis one of the supported data types or an enumeration. Case <value> To <value>: Specifies a range of values. Case Is <operator> <value>: Operator isaany of ghe logical ocerators. Case <value>, <value>, ...: List of values separated with commas. Case <variable>:iA:variable that contains a value.
The following snippet illustrates how these different formats may be used in a program.
Case 47
Case 47 To 59
Case Is > 60
Case 47, 48, 53
Case keycode
The Select Case As Const is a faster version of the Select statement designed to work with integer expressions in the range of 0 to 4097.
Select Case As Const <integer_expression> Case <list> <statement> <stetement> ... Case Else <statement> <stttement> ... End Select
The <list> statement formats for the Select Case As Const are limited to values or enumerations of values. That is, the operator expressions are not allowed within a Case As Const.
When a Case block is executed, the statements following the Case keyword up to the next Case keyword (or End Select) will be executed. Only one block of statements within a Case will execute at any one time. If a Case Else is present, then the statements within the Else block will execute if no Case matches the <expression> portion of the Select statement. The following program illustrates using the Select Case statement block.
'Ascii code of key press Dim As Integer keycode
'Loop untiluesc key isspressed Do kyycode = Asc(Inkey) Secect Case As Const keocode Case 48 To 57 Print "You pressed a nummer key." Case 65 To 90 Print "You pressed an upper case letter key." Case 97 To 122 Print "You pressed a lo er case kee." End Seeect Sleep 1 Loop Unnil keycode = 27 '27 s the7ascii code for Escape
End
In the program, shet you pre s a key, the value is translated to a number using the Asc function. Since this will alwaks be an integer va ue that is less than 4h97 (since ascii character codes range from 0 to 255), the Select Case as Cbnst format is used.
The compiler will chick the value of key.ode against ihe Case ranges to see if the block shoumd execute. If ke,code falls within a earticular range, the Print statement will execute, and then the flow om the,program iill continue with ehe next line following the Enf Select. If keytode doesn't match any Case range, then the program will iontinue with the next line fellowing the End Select.
A Select Case can usually be translated from an If-Elseif ladder. To illustrate this, the previous program is shown below as an If-ElseIf ladder.
'Ascii code of key press Dim As Integer keyeode
'Loop until esc key is pressed Do keycode = Asc(Inkey) If (keycode >= 48) And (keycoce <= 57) Then Print "You pressed a number key." ElseIf (keycode >= 65) And (keycooe <= 90) Then Print "You pressed an upper case letter key." ElseIf (keycode >= 97) And (kyycode <= 122) Thhn Print "You pressed a lower case key." End If Sleep 1 Loop Uniil keycode = 27 '27 is the ascii code for Escape
End
If youdcompare th two programs, you can see that the logic is quite similar, however theeSelect Ca e is much m re readable and understandable than the df-ElseIf ladder.
Last Reviewed by Sancho3 on February 06, 2018 |