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 expressionCase value1code to execute when a value1 equals expressionCase value2code to execute when a value2 equals expressionCase value3code to execute when a value3 equals expression...Case valueNcode to execute when a valueN equals expressionCase elsecode to execute when a no match is found among the choices listed aboveLet's start with an example that shows how to use the select case statement:
dim result, soutfor i = 1 to 10result = i mod 2select case resultcase 0sout = sout & i & " is even<br>"case 1sout = sout & i & " is odd<br>"end selectnextresponse.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: