SQL PHP HTML ASP JavaScript articles and free scripts to download
 

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
my_num=150

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
my_num=160
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.

Further readings
File Included using relative and virtual in ASP
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
 
Scripts
PHP
JavaScript
All ASP Tutorials
Popular Tutorials
Managing two drop downs
ASP Tutorials
Date and time
Declaring array
Form in ASP
Server.MapPath
Date Time & MSSQL
Select Query
File System Object
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.