In the previous lesson you learned how to setup a block of If Statements using the ElseIf keyword,but for checking multiple conditions this is not the most efficient method .
A D V E R T I S E M E N T
To check for multiple Is Equal To "=" conditions of a single variable,ASP uses the Select Case statement.
If you are an experienced programmer you realize the Select statement resembles a Switch statement that other programming languages use for an efficient way to check a large number of conditions at one time
ASP Select Case Example
The list of case statements is checked against the variable that appears immediately after Select Case
Within the Select Case block of code,these case statements are contained
Below is an ASP Select Case example which checks for integer values.
Later we will show how to check for strings
ASP Code:
<%
Dim CaseNum
myNum = 5
Select Case CaseNum
Case 2
Response.Write("CaseNum is Two")
Case 3
Response.Write("CaseNum is Three")
Case 5
Response.Write("CaseNum is Five")
Case Else
Response.Write("CaseNum is " & myNum)
End Select
%>
O/P:
CaseNum is Five
ASP Select Case - Case Else
In the last example you might have noticed something strange, there was a case that was referred to as "Case Else".
This case is actually a catch all option for every case that does not fit into the defined cases.
In english it might be thought of as: I'll use the "Case Else"! if all these cases don't match
It is a good programming practice to always include the catch all Else case. Below we have an example that always executes the Else case
ASP Code:
<%
Dim CaseNum
CaseNum = 454
Select Case CaseNum
Case 2
Response.Write("CaseNum is Two")
Case 3
Response.Write("CaseNum is Three")
Case 5
Response.Write("CaseNum is Five")
Case Else
Response.Write("CaseNum is " & myNum)
End Select
%>
O/P:
CaseNum is 454
Select Case with String Variables
So far we have only used integers in our Select Case statements, but you can also use a string as the variable to be used in the statement.
Below we Select against a string
ASP Code:
<%
Dim CasePet
myPet = "cat"
Select Case CasePet
Case "dog"
Response.Write("I own a dog")
Case "cat"
Response.Write("I do not own a cat")
Case Else
Response.Write("I once had a cute goldfish")
End Select
%>