ASP If Else

VBScript branches code with If...Then...ElseIf...Else...End If, letting an ASP page choose which output to produce based on a condition.

The Basic If Statement

The simplest form tests a condition and runs a statement only when it's True. For a single statement, VBScript allows a compact one-line form: If condition Then statement, with no End If needed. For anything longer than one statement, use the block form, which must be closed with End If on its own line.

Example

<%
  Dim age
  age = 20
  If age >= 18 Then Response.Write("You are an adult.")
%>

Adding an Else Branch

Else supplies the code to run when the condition is False. Combined with the block form of If, this lets a page produce one of two possible outputs depending on a single test.

Example

<%
  Dim age
  age = 15
  If age >= 18 Then
    Response.Write("You may enter.")
  Else
    Response.Write("Sorry, you must be 18 or older.")
  End If
%>

Chaining Conditions with ElseIf

When there are more than two possible outcomes, ElseIf (written as one word) lets you test additional conditions in sequence without nesting a new If inside every Else. VBScript checks each condition from top to bottom and runs the first block whose condition is True, skipping the rest.

Example

<%
  Dim score
  score = 82

  If score >= 90 Then
    Response.Write("Grade: A")
  ElseIf score >= 80 Then
    Response.Write("Grade: B")
  ElseIf score >= 70 Then
    Response.Write("Grade: C")
  Else
    Response.Write("Grade: F")
  End If
%>

Comparison and Logical Operators

OperatorMeaning
=Equal to
<>Not equal to
< and >Less than / greater than
<= and >=Less than or equal to / greater than or equal to
AndTrue only if both sides are True
OrTrue if either side is True
NotReverses True/False
Note: VBScript reuses the = symbol for both assignment (x = 5) and equality comparison (If x = 5 Then) — the engine tells them apart from context. Also remember to write And / Or / Not as words; VBScript has no && or || operators.