Matching options by using Select Case statement in ASP

In VBScript used in ASP, when we have to match a given variable with more number of possible values, it is better to use Select Case statement. Here the first possible matching value is checked and then the code gets executed.

This Select Case type statement is very common in other scripting languages and they are known differently in different scripts. In php the equivalent statement is SWITCH.

Let us start with the syntax of Select Case statement in ASP
Select Case expression
Case value1
	Script part if value1 matches 
Case value2 
	Script part if value2 matches
End Select
Here is the code part in a simple example where a number is stored in a variable and then it is matched with different Cases inside a Select Case statement.
Dim my_num
Select Case my_num
Case 100 
	Response.Write 'It is less than 100" & "<br>"
Case 150
	Response.Write 'It is equal to 150  " & "<br>"
Case  200 
	Response.Write 'It is equal to 200 " & "<br>"
End Select
The output of the above line is 'It is equal to 150'

In the same line while checking for one value we can check for another value by separating it with a coma, like this .
Case 150,160
Response.Write " It is equal to 150 or 160 " & "<br>"
If none of the value matches with the variable we have set then we can keep one default value for this by using Case Else. So in the code below the Case Else statement will be executed.
Dim my_num
Select Case my_num

Case 100
 Response.Write " It is greater than 150 equal to 200" & "<br>"
Case  150 
 Response.Write " It is equal to 150" & "<br>"
Case Else
 Response.Write " It is Not equal to any set values" & "<br>"

End Select
Here the Case Else part will be executed as all other cases will fail to match.

Be the first to post comment on this article :

Further readings
If Else & ElseIF conditional program structure control in ASP
Matching options by using Select Case statement in ASP
Do While Wend Loops in ASP
For Next Loop in ASP

Post your comments , suggestion , error , requirements etc here .

We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer