ASP Select Case

Learn how VBScript's Select Case statement lets you test one variable against many possible values without stacking up a long chain of If...ElseIf checks.

Why Use Select Case?

When you need to check the same variable against several possible values, a long chain of If...ElseIf...ElseIf statements gets hard to read. Select Case lets you list each possible value once, in its own Case block, so the logic reads top to bottom like a menu of outcomes.

Example: Basic Select Case

<%
Dim dayNumber
dayNumber = 3

Select Case dayNumber
    Case 1
        Response.Write("Monday")
    Case 2
        Response.Write("Tuesday")
    Case 3
        Response.Write("Wednesday")
    Case 4
        Response.Write("Thursday")
    Case 5
        Response.Write("Friday")
    Case Else
        Response.Write("Weekend")
End Select
%>

The statement starts with `Select Case` followed by the expression you want to test (dayNumber above). Each `Case` line lists one value to compare it against. VBScript checks the values in order and runs the first block that matches. `Case Else` catches anything that did not match an earlier Case, and `End Select` closes the whole block — it is required, just like `Next` closes a loop or `End If` closes an If block.

  • Every Select Case block must end with End Select.
  • Case Else is optional, but adding one helps you catch values you did not plan for.
  • VBScript checks each Case from top to bottom and stops at the first match — there is no fall-through into the next Case like in some other languages.
  • A single Case can list several values separated by commas.

Example: Grouping Values in One Case

<%
Dim dayNumber
dayNumber = 6

Select Case dayNumber
    Case 1, 2, 3, 4, 5
        Response.Write("It's a weekday - back to work!")
    Case 6, 7
        Response.Write("It's the weekend - relax!")
    Case Else
        Response.Write("Not a valid day number.")
End Select
%>
Note: Select Case always compares the test expression to each Case value using equality. It cannot check a different condition per Case (like "greater than 90" in one branch and "starts with A" in another) — for mixed conditions like that, stick with If...ElseIf.

Matching Strings and Ranges

Select Case works with strings too, not just numbers. By default, VBScript string comparisons are case-sensitive, so "ADD" and "add" are treated as different values. Adding `Option Compare Text` at the very top of your ASP page switches string comparisons to case-insensitive for the rest of that page, which is handy when you cannot guarantee how a value was typed or capitalized.

Example: Case-Insensitive String Matching

<%
Option Compare Text

Dim command
command = "ADD"

Select Case command
    Case "add"
        Response.Write("Adding item to cart.")
    Case "remove"
        Response.Write("Removing item from cart.")
    Case Else
        Response.Write("Unknown command.")
End Select
%>
AspectIf...ElseIfSelect Case
Best forDifferent, unrelated conditionsOne variable checked against many values
ReadabilityGets messy with many branchesReads like a clean list of outcomes
Multiple matchesNeeds Or inside each conditionList values in one Case, comma-separated
Rangescondition >= 1 And condition <= 5Case 1 To 5
Note: Select Case also understands ranges and comparisons: `Case 1 To 10` matches any number from 1 to 10 inclusive, and `Case Is > 90` matches any value greater than 90. `Case Is` always compares against the Select Case expression itself, not a second variable.