ASP - If Statement
An If Statement is used to make a decision in your ASP program to execute
certain code if some condition is True. Because ASP is programmed in VBScript by default,
when you program an ASP If Statement it is actually the same as programming a VBScript If Statement.
If Statement Syntax
ASP's If Statement is slightly different than the If Statement implementation in
most other languages. There are no brackets, or curly braces, nor are there any
parenthesis. Rather the beginning of the code to be executed in the If Statement when its true
is marked with Then and the end of the If Statement is plainly marked with
End If. Below is a very basic If Statement that will always be True.
ASP Code:
<%
Dim myNum
myNum = 6
If myNum = 6 Then
Response.Write("Variable myNum = 6")
End If
%>
Display:
Variable myNum = 6
You might notice that the "=" operator is used to both set the value of myNum to 6 at first, then it is
used to compare myNum to 6 in our If Statement. This dual use of the equals operator is confusing to many, but
it might help you to remember that in ASP you cannot set the value of variables within If Statements, which means that the "=" can only compare!
ASP - If Else Conditional Statement
Sometimes you might want to execute some code both when the If Statement is True and some different code when it is False. Just like
other programming languages, you can do this in ASP with the use of the Else keyword. Below is an example that will always be false, so that
the Else portion of the If Statement is always executed.
ASP Code:
<%
Dim myNum
myNum = 23
If myNum = 6 Then
Response.Write("Variable myNum = 6")
Else
Response.Write("**Variable myNum = " & myNum)
End If
%>
Display:
**Variable myNum = 23
ASP - ElseIf Conditional Statement
With a normal If Statement you can only check one condition, but at times you will want to check
for multiple conditions. In ASP you can do this with ElseIf, which is the name given to an If Statement that depends on another If Statement.
Think about it in plain english: If something is true Then do this ElseIf second something is true Then do this, etc.
You may have used the ElseIf condition statement in other programming languages,
but if not just know that you cannot have an ElseIf statement without first having an if statement.
Below is an example whose second if statement (elseif) is always true.
ASP Code:
<%
Dim myFastfood
myFastfood = "JBox"
If myFastfood = "McD's" Then
Response.Write("Happy Meal Por Favor!")
ElseIf myFastfood = "JBox" Then
Response.Write("Two tacos please!")
Else
Response.Write("Foot-long turkey sub.")
End If
%>
Display:
Two tacos please!
Found Something Wrong in this Lesson?Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time! |