Use Select Case in Classic ASP (VBScript) when one value must be checked against several possible options. The matching Case runs its statements; Case Else handles unexpected values.
Dim my_num
my_num = 150
Select Case my_num
Case 100
Response.Write "It is equal to 100"
Case 150
Response.Write "It is equal to 150"
Case 200
Response.Write "It is equal to 200"
Case Else
Response.Write "No matching value"
End Select
Output:
It is equal to 150
The number 150 matches the second Case, so only its message is displayed. Select Case is a readable alternative to several If...ElseIf conditions when all branches examine the same expression.
Select Case expression
Case value1
' statements when expression = value1
Case value2, value3
' statements when expression = value2 or value3
Case Else
' statements when nothing matches
End Select
| VBScript statement | Purpose |
|---|---|
Select Case expression | Evaluates the value to test. It may be a number, string, or expression. |
Case value | Matches one value. |
Case value1, value2 | Matches either of the comma-separated values. |
Case Else | Optional fallback if none of the earlier cases match. |
End Select | Ends the decision block. |
The first matching Case wins. After its statements run, execution continues after End Select; unlike some languages, VBScript does not need a break statement after each branch. If no case matches and there is no Case Else, the block produces no action.
The original tutorial compares my_num = 150 with three numeric choices. Here it is with consistent indentation and the same output.
Dim my_num
my_num = 150
Select Case my_num
Case 100
Response.Write "It is equal to 100" & "<br>"
Case 150
Response.Write "It is equal to 150" & "<br>"
Case 200
Response.Write "It is equal to 200" & "<br>"
End Select
Output:
It is equal to 150
If my_num were 999, this example would display nothing, because it has no matching branch and no fallback.
A comma separates alternatives. This keeps the logic short when two or more values should trigger the same response.
Dim my_num
my_num = 160
Select Case my_num
Case 100
Response.Write "It is equal to 100"
Case 150, 160
Response.Write "It is equal to 150 or 160"
Case Else
Response.Write "Other number"
End Select
Output:
It is equal to 150 or 160
Dim answer
answer = "y"
Select Case answer
Case "yes", "y"
Response.Write "Confirmed"
Case "no", "n"
Response.Write "Declined"
Case Else
Response.Write "Enter yes or no"
End Select
Output:
Confirmed
A Case list checks for any of the specified values. It does not test whether part of a string contains a word; for substring searches, use InStr().
The original page uses my_num = 160 while its listed cases are 100 and 150. The fallback therefore runs.
Dim my_num
my_num = 160
Select Case my_num
Case 100
Response.Write "It is equal to 100" & "<br>"
Case 150
Response.Write "It is equal to 150" & "<br>"
Case Else
Response.Write "It is not equal to any listed value" & "<br>"
End Select
Output:
It is not equal to any listed value
Place Case Else after the other cases, and use it for unexpected input rather than silently accepting unknown values.
When a visitor may enter uppercase letters or surrounding spaces, normalize the text before checking the options. LCase() converts text to lowercase and Trim() removes surrounding spaces.
Dim status, message
status = " ACTIVE "
Select Case LCase(Trim(status))
Case "active"
message = "Account is active"
Case "pending"
message = "Awaiting approval"
Case "closed"
message = "Account is closed"
Case Else
message = "Unknown account status"
End Select
Response.Write message
Output:
Account is active
Dim status
status = "processing"
Select Case LCase(Trim(status))
Case "new", "processing", "packed"
Response.Write "Order in progress"
Case "shipped", "delivered"
Response.Write "Order on its way or completed"
Case Else
Response.Write "Status not recognized"
End Select
Output:
Order in progress
Using LCase() makes the accepted casing explicit; this matters when text comes from a submitted ASP form.
Case 1 To 10 or Case Is > 10 syntax. For ranges, evaluate Boolean conditions using Select Case True, or use If...ElseIf...Else.In the following example, every Case condition returns True or False. The first True case provides the grade.
Dim marks, grade
marks = 78
Select Case True
Case marks >= 90 And marks <= 100
grade = "A"
Case marks >= 75 And marks < 90
grade = "B"
Case marks >= 60 And marks < 75
grade = "C"
Case marks >= 0 And marks < 60
grade = "Needs improvement"
Case Else
grade = "Marks must be from 0 to 100"
End Select
Response.Write grade
Output:
B
Keep the ranges in a sensible order, especially if some conditions overlap. Here marks = 78 meets the 75–89 condition. Each comparison is written out explicitly so the boundaries are unambiguous.
Dim number
number = 12
Select Case number Mod 2
Case 0
Response.Write "Even number"
Case 1
Response.Write "Odd number"
End Select
Output:
Even number
For nonnegative whole numbers, Mod 2 gives 0 for even and 1 for odd values. Use If to validate any visitor-entered number before applying numeric operations.
Use a second Select Case inside a branch if a decision depends on another option.
Dim paymentMethod, currency
paymentMethod = "card"
currency = "USD"
Select Case paymentMethod
Case "card"
Select Case currency
Case "USD"
Response.Write "Card payment in US dollars"
Case "INR"
Response.Write "Card payment in rupees"
Case Else
Response.Write "Unsupported card currency"
End Select
Case "cash"
Response.Write "Cash payment"
Case Else
Response.Write "Unknown payment method"
End Select
Output:
Card payment in US dollars
You can classify each item in a VBScript array using the same decision block.
Dim statuses, status
statuses = Array("new", "pending", "done")
For Each status In statuses
Select Case status
Case "new"
Response.Write "New order<br>"
Case "pending"
Response.Write "Waiting<br>"
Case "done"
Response.Write "Completed<br>"
Case Else
Response.Write "Other<br>"
End Select
Next
Output:
New order
Waiting
Completed
The For Each loop visits the values in sequence and runs one branch for each. The <br> tags create line breaks in the rendered page.
Save the following code in a separate select-case-form.asp test file and open it on an IIS server with Classic ASP enabled. This example accepts a POST value, normalizes it, selects a message, and HTML-encodes displayed text.
<%@ Language="VBScript" %>
<% Option Explicit %>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>Select Case Form</title></head>
<body>
<h1>Choose a category</h1>
<form method="post" action="">
<label for="category">Category:</label>
<select name="category" id="category">
<option value="books">Books</option>
<option value="games">Games</option>
<option value="toys">Toys</option>
</select>
<button type="submit">Show category</button>
</form>
<%
Dim selectedCategory, message
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
selectedCategory = LCase(Trim(Request.Form("category")))
Select Case selectedCategory
Case "books"
message = "Browse our books"
Case "games"
message = "Browse our games"
Case "toys"
message = "Browse our toys"
Case Else
message = "Please choose a valid category"
End Select
Response.Write "<p>" & Server.HTMLEncode(message) & "</p>"
End If
%>
</body>
</html>
The Case Else branch is still needed even though the visible dropdown has only three options: an HTTP client can submit a different value. Validate all server-side input before using it in queries or other operations.
For ranges, first make sure a submitted value is numeric. This standalone VBScript segment assumes it is placed inside a <% ... %> block on a Classic ASP page.
Dim rawMarks, marks, grade
rawMarks = Trim(Request.Form("marks"))
If IsNumeric(rawMarks) Then
marks = CDbl(rawMarks)
Select Case True
Case marks < 0 Or marks > 100
grade = "Invalid marks"
Case marks >= 75
grade = "Pass with distinction"
Case marks >= 40
grade = "Pass"
Case Else
grade = "Needs improvement"
End Select
Else
grade = "Enter a numeric mark"
End If
Response.Write Server.HTMLEncode(grade)
This illustrates input validation and conditional classification. Decimal conversion depends on the server locale; use a predictable input format and explicit validation when the exact numeric representation matters.
| Issue | What to check |
|---|---|
| No output | No Case matched and Case Else is missing. Add a fallback or check the input value. |
| Unexpected branch | Only the first matching case runs. Check ordering and overlapping conditions. |
| Case does not match typed text | Check whitespace, letter case, and the source of the submitted value; use LCase(Trim(...)) when appropriate. |
Case 1 To 10 causes a syntax error | That VBA-style range is not supported in VBScript. Use Select Case True with a Boolean comparison or use If...ElseIf. |
| Numeric comparison error | Validate with IsNumeric() before attempting CDbl() or numeric comparisons. |
| Page shows the literal ASP source | Run a .asp page through IIS with Classic ASP enabled, rather than opening the file directly in a browser. |
No. When more than one Case could match, only the first matching branch runs. Processing then continues after End Select.
No. It is optional, but it is useful for invalid, missing, and unrecognized values. Without it, an unmatched value runs no branch.
Yes. Separate values with commas, for example Case 150, 160 or Case "yes", "y".
Yes, by using Select Case True with conditions such as Case marks >= 75. Do not copy Case Is > 75 from VBA examples into Classic ASP VBScript.
Select Case is a good fit when you are evaluating one expression against many alternatives. If...ElseIf is often clearer when unrelated conditions, complex logic, or several variables need to be tested.
No. VBScript does not fall through to the next branch. Do not add a JavaScript- or PHP-style break statement.
For further syntax details, see the VBScript Select Case reference.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.