Vbscript If Then Else In ASP

Updated on 20 Feb 2012,
Published on 28 Jul 2008

In ASP If Then Else conditional statements of Vbscript provides the functionality to test the condition and if it returns true then executes the code block inside it otherwise exits the code. Vbscript if then else can be used to decide the code execution according to the condition provided for it. In Vbscript there are three types of if then else conditional statements:

1. if then statement:

if statement is used to run the code only when the condition is true and there is no alternate code to be executed when the condition is false.

2. if then else statement:

if there are two types of codes that you want to execute conditionally when if statement returns true then first code is executed and when it returns false other part of code is executed.

3. if then elseif statement:

if there are more than two conditional statement, there are number of conditional criteria and you want to execute only one code block according to the condition then if then elseif conditional statements are used.

Classic ASP VbScript Basics Examples:

You can see the live samples and examples of Classic ASP VbScript Basics from the following links:

Examples for Vbscript if then else in ASP

If Then Statement Example:
<%

Dim a,b

a = 10

b = 20

If b > a Then
    Response.Write "Yes b is greater than a."
End If

%> 

If statement ends with End If statement.

If Then Else statement Example:
<%

Dim a,b

a = 10

b = 20

If b > a Then
    Response.Write "b is greater than a."
Else
    Response.Write "a is greater than b."
End if

%>
If Then ElseIf Statement Example:
<%

Dim grade

If grade="A" Then
    Response.Write "Brilliant"
ElseIf grade="B" Then
    Response.Write "Excellent"
ElseIf grade="C" Then
    Response.Write "Good"
ElseIf grade="D" Then
    Response.Write "Satisfactory"
Else
    Response.Write "Fail"
End If

%>

Output:

You can see the output of above discussed code from the following links:

VbScript If Condition

VbScript If-Else Condition

VbScript If-Else-If Conditions

Continue to next tutorial: Vbscript Select Case in ASP  to learn about Select case statements that enable you to replace the multiple If-ElseIf conditions and make the code more readable.

1 Responses to "Vbscript If Then Else In ASP"
12
its helpful
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers