Building the Right Environment to Support AI, Machine Learning and Deep Learning
Get Off My Case!
Like the If...Then statement, the Select Case statement is used for decision-making. Select Case enables you to do various comparisons against one variable that you use throughout the whole statement.
A simple case
Here's a simple Select Case statement that checks an employee's status and displays the result to the user:
Public Sub Main()
Dim EmployeeStatus as String
EmployeeStatus = "L"
Select Case EmployeeStatus
Console.WriteLine("Employee is in good standing.")
Console.WriteLine("Employee is on leave.")
Console.WriteLine("Employee no longer works here.")
Here's how this Select Case statement works:
- Always begin your Select Case statements with the
words Select Case. (Easy enough?)
- After that comes a variable name. Here, the variable
is EmployeeStatus. This variable is used throughout the rest of the Select
- Next comes a series of lines that each contain the
word Case followed by a value. Select Case automatically compares the first
value ("G") to the variable at the beginning of the Select Case statement
(EmployeeStatus) to see if they are equal.
- If the first value is equal to the variable at the
beginning, the line after Case "G" and before Case "L" is executed. After that
line is finished, the statement is done and any other Case lines are ignored.
Anything after the End Select gets processed next.
- If the first value isn't equal to the variable at the top, the next Case statement is checked (Case "L") and that value is compared with the variable at the beginning (again, EmployeeStatus). If a match exists, the lines under Case "L" are executed. If not, VB.NET proceeds to the next Case statement.
It's possible that none of the Case statements may match. If that happens, none of the lines within the Select Case get executed, and you just go on after the End Select. This situation would happen in the preceding statement if the EmployeeStatus was something other than "G," "L," or "F."
Notice that you don't need to repeat the variable name or even the equal sign again and again as you would in an If...Then...ElseIf. Both are automatically assumed. This strategy makes the Select Case much cleaner and easier to understand if you are continually comparing against one variable.
A tougher case
The Select Case statement is very flexible. It can do more than simple checks for equality. Like If...Then statements, it can do inequalities (with the <, >, <=, >=, and <> operators).
Here's the letter grade calculator I demonstrate earlier in this article. However, instead of using If...Then...ElseIf, I've converted the code to use Select Case:
Public Sub Main()
Dim Grade As Integer
Dim LetterGrade As String
Grade = Int(Rnd * 100) + 1 ' Random number - 1 and 100
Console.WriteLine("Your grade is " & Grade)
Select Case Grade
Case Is >= 90
LetterGrade = "A"
Console.WriteLine("You got an A! Congratulations.")
Case Is >= 80
LetterGrade = "B"
Console.WriteLine("You got a B. Good job.")
Case Is >= 70
LetterGrade = "C"
Console.WriteLine("You got a C. Not bad.")
Case Is >= 60
LetterGrade = "D"
Console.WriteLine("You got a D. Try harder...")
LetterGrade = "F"
Console.WriteLine("You failed. I'm sorry.")
Notice that, when comparing to see if something is equal in a Select Case, you only have to put the value on the Case line:
However, when you check for inequality, you have to use the keyword Is and you have to include the inequality operator you want to use:
Case Is >= 90
Using the keyword Is here may seem odd, but that's how it works!
You also can do some other fancy comparisons in the Case line. For example, if you were checking a value that could be any integer between 1 and 6, you might have a case line that looks like this:
Case 3 To 6
This will match if the variable is 3, 4, 5, or 6.
If you want to specify several values, use commas:
Case 2, 4, 6, 8
You can even combine the two techniques:
Case 1,3, 4 To 6
This would match on 1, 3, 4, 5, or 6.
When Should You Use Select Case Instead of If...Then...ElseIf?
As you may have noticed, you can create an If...Then...ElseIf that works very much like a Select Case statement, and vice versa. So, which one should you use?
In some cases, it doesn't matter. You can use either one. But, remember that If...Then...ElseIf is more flexible. Select Case restricts you to doing comparisons to only one value—the one that comes after Select Case. In an If...Then...ElseIf, you can use compound conditions to compare against two or three values, or as many as you like.
So, if you're doing several comparisons against one variable, using the Select Case is probably easier. But, if you need to do more complex comparisons, resort to If...Then...ElseIf.
In some computer languages, the Select Case statement actually runs faster than an If...Then...ElseIf. If you have programmed in C or C++, you know that this is true for those languages—and because of that factor, you are encouraged to use Select Case whenever possible. In VB.NET, you'll find no significant difference in execution speed between the two statements. So, use whichever makes your code easier to understand.
In this installment, you raised the IQ of your programs significantly by showing them how to make decisions on their own with If...Then...Else and Select Case statements. In the next article, you'll discover how loops can do the heavy lifting for calculations and a host of other tasks.