If, ElseIf and Else in VBScript

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.

Quick rule: use If...Then for one condition, Else for the alternative, and ElseIf when several conditions need to be tested in order.

Quick example: If...Then...Else

<%
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...Then...Else syntax

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.

StatementPurpose
If...ThenRuns a block when a condition is true.
ElseIf...ThenChecks another condition if earlier conditions were false.
ElseRuns if no earlier branch matches.
End IfCloses a multiline conditional block.

Find the greatest of two numbers

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.

If without Else

<%
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.

Nested If statements

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.

Use ElseIf for multiple conditions

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.

Order of ElseIf conditions matters

<%
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.

Comparison and logical operators

OperatorMeaningExample
=Equalage = 18
<>Not equalname <> ""
>, <Greater than, less thanmarks > 50
>=, <=At least, at mostmarks <= 100
AndBoth conditions must be truescore >= 0 And score <= 100
OrAt least one condition must be trueday = "Sat" Or day = "Sun"
NotReverses a Boolean conditionNot isReady

Check a range with And

<%
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
%>

Check alternatives with Or

<%
Dim dayName
dayName = "Sunday"
If dayName = "Saturday" Or dayName = "Sunday" Then
    Response.Write "Weekend"
Else
    Response.Write "Weekday"
End If
%>
Important: VBScript 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.

Single-line If statements

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.

Use If with form values in Classic ASP

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.

Validate a number before converting it

<%
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.

Using If inside a loop

<%
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.

Common mistakes and troubleshooting

ProblemWhat to check
Missing End IfEach multiline If block must have its own closing End If.
Using Else If for a branchUse ElseIf as one word for a single multi-branch block.
Unexpected branch runsCheck the order of conditions and whether an earlier condition already matches.
Type mismatch on form inputValidate input before converting it to a number or date.
Testing for NullUse IsNull(value), not value = Null.
Confusing = with assignmentVBScript uses = for both, depending on whether it appears in a comparison or assignment statement; == is not VBScript syntax.

Handle a Null value

<%
Dim valueToCheck
valueToCheck = Null
If IsNull(valueToCheck) Then
    Response.Write "No value available"
Else
    Response.Write "Value is present"
End If
%>

Frequently asked questions

Can I use If without Else?

Yes. Use If condition Then ... End If when nothing needs to happen if the condition is false.

What is the difference between ElseIf and nested If?

ElseIf tests alternative branches of one decision. A nested If tests another condition inside a branch that has already been selected.

Does Classic ASP support ElseIf?

Yes. ElseIf is part of VBScript's multiline If...Then...Else statement.

When should I use Select Case instead?

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.

Does ElseIf require an extra End If?

No. One chain of If...ElseIf...Else ends with one End If. Nested blocks each require their own End If.

Try these changes

  1. Change n1 and n2 to equal numbers and observe the result.
  2. Change score to 49, 75, and 92 to test different ElseIf branches.
  3. Remove the name from the form and submit to see the server-side validation message.

ASP tutorial index   |   Microsoft If...Then...Else reference

Back to top ↑


ASP Home






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