VBScript Select Case
The Select Case statement can be used instead of an If statement if you have many conditions to check for.
In the previous lesson, we used an
Elseif statement to perform multiple checks against the value of a variable. VBScript Select Case statements are a more efficient way of doing this.
Select Case statement allows us to perform multiple tests, with less code (and less computing power). This can be especially beneficial when you have many cases to test for.
Example Select Case Statement
<script type="text/vbscript"> Dim hairColor hairColor = "Yellow" Select Case hairColor Case "Black" document.write("Same as the cat") Case "Blue" document.write("Same as the parrot") Case "Yellow" document.write("Same as the canary") Case Else document.write("Ah well, whatever...") End Select </script>
This results in:
Limitations of Select Case
Now, you may be thinking, "if Select Case is so efficient, why use
If Elseif at all?".
Select Case statement is more efficient than an
If Elseif statement for this type of thing, it is somewhat limited - there may be times that you need to use an
If Elseif statement. For example, the
Select Case statement only allows you to check if a variable is equal to a value - you can't test to see if it is greater than, less than etc. Also, you can only test against one variable. If you have multiple variables to test against, you'll need to use an