Select case statements

Instead of using an ELSE...IF structure, you can use a select case statement to execute one statement among many alternatives. The basic idea behind using a select statement is to give an expression to evaluate and several different statements to execute based on the value of the expression. Each case in a select statement is checked against value of the expression until a match is found. Once a match is found, the execution of the select case statement ends. If no match is found, a default case, if specified, is executed. The basic syntax for a select case statement is:

Select case expression
Case value1
code to execute when a value1 equals expression
Case value2
code to execute when a value2 equals expression
Case value3
code to execute when a value3 equals expression
...
Case valueN
code to execute when a valueN equals expression
Case else
code to execute when a no match is found among the choices listed above

Let's start with an example that shows how to use the select case statement:

dim result, sout
for i = 1 to 10
result = i mod 2
select case result
case 0
sout = sout & i & " is even<br>"
case 1
sout = sout & i & " is odd<br>"
end select
next
response.write sout

In this example we use a for loop to iterate the variable i from 1 through 10. With each value for i, we determine if the value is divisible by 2. On line 4, we start our select case statement and our expression is the value of result. The value of result is going to be either 0 or 1. As the loop and select case statement executes, the value of result will be checked with 0, and 1; see lines 5 and 7, respectively. The following shows the output of the above code:

1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even