Having the ability to perform different actions for based on certain conditions is a critical component of any programming language. One of the ways that you can have your program execute code based on a
decision is with conditional statements. Conditional statements are the set of commands used to perform different actions based on different conditions. In ASP, we use VBScript code to implement several types of conditional
statements. All of the conditional statements produce a similar result but may be used in different ways depending on the conditions that are being evaluated.
- If Then
- If Then...Else
- If Then...ElseIf...Else
- Select Case
HTML Sample
<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<%
'ASP code block
%>
</body>
</html>
If Then
The simplest technique that can be used is to validate a whether a condition is TRUE. You can execute this using one line of VBScipt code.
<%
If x > 1 Then Response.Write("x is greater than 1!")
%>
If you need to execute more than one line of VBScript code, then you write the additional lines of code under the If Then Statement and terminate the block with an EndIf keyword.
<%
If x > 1 Then
'execute this block when x is greater than 1
Response.Write("x is greater than 1!")
EndIf
%>
If Then...Else
If you want to execute a statement if a condition is TRUE and execute another statement if the condition is FALSE, you can use the "Else" keyword:
<%
If x > 1 Then
Response.Write("x is greater than 1!")
Else
Response.Write("x is not greater than 1!")
EndIf
%>
If Then...ElseIf
The If...Then...ElseIf statements are used like the If...Then...Else expression, except that you can run through multiple comparisons. In this scenario, the process will first examine Condition-1. If Condition-1 is
TRUE, the script will execute the first block of code and exit the process. If Condition-1 is FALSE, the process will continue to examine Condition-2. The same process continues. Whenever a condition is FALSE, the
process will continue examining the conditions until it finds one that is TRUE or reaches the ELSE keyword. Once a TRUE condition has been found, its code is executed and the process exits.
If none of the conditions are found as TRUE, the process will execute the code found in the ELSE block, if the ELSE keyword is included before the End If.
<%
If x > 1 Then
Response.Write("x is greater than 1!")
ElseIf x < 1
Response.Write("x is not greater than 1!")
EndIf
%>
<%
If x > 1 Then
Response.Write("x is greater than 1!")
ElseIf x < 1
Response.Write("x is less than 1!")
Else
Response.Write("I think x is 1!")
EndIf
%>
Select Case
The Select Case statements work in the same manner as If statements. You use the Select Case when you need to check for multiple values, not TRUE or FALSE. The Select Case statement allows a program to evaluate
an expression and attempts to match the value of the expression to a specific Case. If a match is found, the program executes the associated statement within that Case. If no match is found, the program looks for the
optional Case Else clause. If it exists, it executes the code within the Case Else block. If no Case Else statement is found, the process exits and the program continues to execute the next statement following the End
Select.
<%
dim d
d=weekday(date)
Select Case d
Case 1
Response.Write("Sunday")
Case 2
Response.Write("Monday")
Case 3
Response.Write("Tuesday")
Case 4
Response.Write("Wednesday")
Case 5
Response.Write("Thursday")
Case 6
Response.Write("Friday")
Case Else
Response.Write("Saturday")
End Select
%>
Did you find the page informational and useful? Share it using one of your favorite social sites.
Recommended Books & Training Resources