Use If...Then...Else in Classic ASP (VBScript) to run different statements depending on a condition. The same structure supports ElseIf for multiple tests and nested If statements for more detailed decisions.
If...Then for one condition, Else for the alternative, and ElseIf when several conditions need to be tested in order.<%
Dim number
number = 25
If number >= 18 Then
Response.Write "The number is 18 or greater"
Else
Response.Write "The number is below 18"
End If
%>
Output: The number is 18 or greater. The Else branch runs only when the first condition is false.
If condition Then
' Statements when the condition is True
Else
' Statements when the condition is False
End If
Classic ASP evaluates the condition first. Only the matching branch is executed. The Else branch is optional; a multiline If block finishes with End If.
If condition1 Then
' First branch
ElseIf condition2 Then
' Second branch
Else
' Fallback branch
End If
ElseIf is written as a single keyword. Once a branch matches, the remaining ElseIf and Else branches are skipped.
| Statement | Purpose |
|---|---|
If...Then | Runs a block when a condition is true. |
ElseIf...Then | Checks another condition if earlier conditions were false. |
Else | Runs if no earlier branch matches. |
End If | Closes a multiline conditional block. |
The original tutorial compares 25 and 45. This version also handles the case where both numbers are equal.
<%
Dim n1, n2
n1 = 25
n2 = 45
If n1 > n2 Then
Response.Write "Greatest number is " & n1
ElseIf n2 > n1 Then
Response.Write "Greatest number is " & n2
Else
Response.Write "Both numbers are equal"
End If
%>
Output: Greatest number is 45. Change the values of n1 and n2 to test every branch.
<%
Dim temperature
temperature = 35
If temperature > 30 Then
Response.Write "Hot day"
End If
%>
When the condition is false, no message is printed. You do not need an Else branch for every If.
An If can appear inside another If. Each multiline If needs its own End If. In this example, the original values −25 and −45 are retained.
<%
Dim n1, n2
n1 = -25
n2 = -45
If n1 > n2 Then
Response.Write "Greatest number is " & n1 & "<br>"
If n1 < 0 Then
Response.Write "The first number is negative"
End If
Else
Response.Write "Greatest number is " & n2
End If
%>
Output: Greatest number is -25, followed by The first number is negative. The inner condition is evaluated only because the outer condition is true.
The original tutorial uses my_num = 40. The following version retains its two nested messages while giving the remaining branches accurate boundary descriptions.
<%
Dim my_num
my_num = 40
If my_num < 100 Then
Response.Write "It is less than 100<br>"
If my_num < 50 Then
Response.Write "It is less than 50 also"
End If
ElseIf my_num > 200 Then
Response.Write "It is greater than 200"
Else
Response.Write "It is between 100 and 200 (inclusive)"
End If
%>
Output: It is less than 100, then It is less than 50 also. For values from 100 to 200 inclusive, the final Else branch is used.
<%
Dim score
score = 85
If score >= 90 Then
Response.Write "Grade A"
ElseIf score >= 75 Then
Response.Write "Grade B"
ElseIf score >= 50 Then
Response.Write "Grade C"
Else
Response.Write "Needs improvement"
End If
%>
Output: Grade B. Test the most restrictive condition first; otherwise an earlier, broader match can prevent a later branch from running.
| Operator | Meaning | Example |
|---|---|---|
= | Equal | age = 18 |
<> | Not equal | name <> "" |
>, < | Greater than, less than | marks > 50 |
>=, <= | At least, at most | marks <= 100 |
And | Both conditions must be true | score >= 0 And score <= 100 |
Or | At least one condition must be true | day = "Sat" Or day = "Sun" |
Not | Reverses a Boolean condition | Not isReady |
<%
Dim marks
marks = 74
If marks >= 0 And marks <= 100 Then
Response.Write "Valid marks"
Else
Response.Write "Marks must be between 0 and 100"
End If
%>
<%
Dim dayName
dayName = "Sunday"
If dayName = "Saturday" Or dayName = "Sunday" Then
Response.Write "Weekend"
Else
Response.Write "Weekday"
End If
%>
And and Or do not provide short-circuit evaluation. If a value needs checking before conversion or property access, use nested If statements so the risky operation is attempted only after validation.A short condition can fit on one line. Unlike a multiline block, this form does not use End If.
<%
Dim visits
visits = 5
If visits > 0 Then Response.Write "Welcome back"
%>
For multiple statements or complex conditions, prefer the multiline version because the control flow is easier to read.
The following complete .asp example checks whether a visitor entered a name. Place this code in an ASP file running on IIS. Submitted input is encoded before being displayed.
<%
Option Explicit
Dim visitorName
visitorName = Trim(Request.Form("visitorName"))
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
If Len(visitorName) > 0 Then
Response.Write "Welcome, " & Server.HTMLEncode(visitorName)
Else
Response.Write "Please enter your name."
End If
End If
%>
<form method="post" action="">
<label for="visitorName">Your name</label>
<input type="text" id="visitorName" name="visitorName" required>
<button type="submit">Submit</button>
</form>
The HTML required attribute improves the browser experience, but the server must still check submissions. Learn more in Classic ASP forms and GET versus POST.
<%
Dim rawNumber, userNumber
rawNumber = Trim(Request.Form("amount"))
If IsNumeric(rawNumber) Then
userNumber = CDbl(rawNumber)
If userNumber >= 0 Then
Response.Write "Amount accepted"
Else
Response.Write "Enter a non-negative amount"
End If
Else
Response.Write "Enter a valid number"
End If
%>
The nested condition checks IsNumeric() before calling CDbl(). For real payments, add stricter format, range, and business-rule validation.
<%
Dim i
For i = 1 To 6
If i Mod 2 = 0 Then
Response.Write i & " is even<br>"
Else
Response.Write i & " is odd<br>"
End If
Next
%>
This pattern is useful when deciding how to process each item. See For...Next loops and Do...Loop structures.
| Problem | What to check |
|---|---|
Missing End If | Each multiline If block must have its own closing End If. |
Using Else If for a branch | Use ElseIf as one word for a single multi-branch block. |
| Unexpected branch runs | Check the order of conditions and whether an earlier condition already matches. |
| Type mismatch on form input | Validate input before converting it to a number or date. |
Testing for Null | Use IsNull(value), not value = Null. |
Confusing = with assignment | VBScript uses = for both, depending on whether it appears in a comparison or assignment statement; == is not VBScript syntax. |
<%
Dim valueToCheck
valueToCheck = Null
If IsNull(valueToCheck) Then
Response.Write "No value available"
Else
Response.Write "Value is present"
End If
%>
Yes. Use If condition Then ... End If when nothing needs to happen if the condition is false.
ElseIf tests alternative branches of one decision. A nested If tests another condition inside a branch that has already been selected.
Yes. ElseIf is part of VBScript's multiline If...Then...Else statement.
Use Select Case when a single expression needs to be compared with several known options; use If...ElseIf for different kinds of conditions or ranges.
No. One chain of If...ElseIf...Else ends with one End If. Nested blocks each require their own End If.
n1 and n2 to equal numbers and observe the result.score to 49, 75, and 92 to test different ElseIf branches.ASP tutorial index | Microsoft If...Then...Else reference
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.