2. The Visual Basic Language
Select Case - Another Way to Branch
- In addition to If/Then/Else type statements, the Select Case
format can be used when there are multiple selection possibilities.
- Say we've written this code using the If statement:
If Age = 5 Then
Category = "Five Year Old"
ElseIf Age >= 13 and Age <= 19 Then
ElseIf (Age >= 20 and Age <= 35) Or Age = 50 Or (Age >= 60 and
Age <= 65) Then
Category = "Special Adult"
ElseIf Age > 65 Then
Category = "Senior Citizen"
Else
Category = "Everyone Else"
End If
The corresponding code with Select Case would be:
Select Case Age
Case 5
Category = "Five Year Old"
Case 13 To 19
Case 20 To 35, 50, 60 To 65
Category = "Special Adult"
Case Is > 65
Category = "Senior Citizen"
Case Else
Category = "Everyone Else"
End Select
Notice there are several formats for the Case statement.
Consult on-line help for discussions of these formats.
Counter Hit
This Homepage is special brought to you by CK Tan