The Wayback Machine - https://web.archive.org/all/20041212025635/http://www.aspphptutorials.com:80/ASP/if-then-elseif-tutorial.php


ASP Tutorials
Arrays
Cookies
Date & Time
Display Text
Do While
For Next
If Then ElseIf
Introduction
Select Case
Variables
 
PHP Tutorials
Date & Time
Display Text
Do While
For Loop
If Else
Switch Case
Variables
 
Directory
ASP
Beginner
Intermediate
Advanced

PHP
Beginner
Intermediate
Advanced
 

ASP Tutorials
 
 

If, Else, ElseIf Statements

"If" statement is a control structure to determine the flow of the ASP page, using conditions given by user. The basic sytax is like this:
If -condition- Then
-execute this-
Else
-execute something else-
End If

This code translates as: "If -condition- is true then execute -this-, if not then execute -something else-". "If" statement can be extended using "ElseIf" statement. The syntax is like this:
If -condition- Then
-execute this-
ElseIf -another condition- Then
-execute that-
Else
-execute something else-
End If

This code means: "If -condition- is true then execute -this-, if not true but -another condition- is true then execute -that-, if both conditions are not met then execute -something else-". You can use several "ElseIf" statements to check several specific conditions:
If -condition 1- Then
-execute code 1-
ElseIf -condition 2- Then
-execute code 2-
ElseIf -condition 3- Then
-execute code 3-
ElseIf -condition 4- Then
-execute code 4-
Else
-execute something else-
End If

"ElseIf" and "Else" statements are not required, you can use "If" alone:
If -condition- Then
-execute this-
End If

Examples:
<%
Dim myAge
myAge = 25 If myAge = 30 Then
Response.Write "It's 30"
ElseIf myAge < 30 Then
Response.Write "I'm younger then 30"
Else
Response.Write "I'm neither 30 nor younger then 30"
End If
%>

This code will give the following result:
I'm younger then 30

VBScript has the following comparison operators to use in condition checking: = : Equal to
< : Less than
<= : Less than or equal to
> : Greater than
>= : Greater than or equal to
<> : Not equal to You can use "AND" and "OR" operators to check more then one condition in every "If"/"ElseIf" statement. For example:
<%
Dim myName, myAge
myName = "John" myAge = 30 If myName = "John" AND myAge = 30 Then
Response.Write "My name is John and I'm 30"
ElseIf myName = "John" OR myAge = 30 Then
Response.Write "Either my name is John or I'm 30"
Else
Response.Write "My name is not John and I'm not 30"
End If
%>

The code executed after condition checking is not limited to one line, there could be limitless counts of lines executed. For example:
<%
Dim myName
myName = "John" If myName <> "John" Then
Response.Write "My "
Response.Write "name "
Response.Write "is "
Response.Write "not "
Response.Write "John"
End If
%>