Go to: Articles List
There are two types of conditional logic in ASP: if and select. I will
show you how to use both of them. In Conditionals Part 2, I will discuss
when and why to use each of them.
An if statement is used to help a programmer determine whether to
execute one set of code or another. The syntax for an if statement is
the following:
If (condition) Then
'some code
Else
'some other code
End If
Example:
If Num1 > Num2 Then
Response.Write("Num1 is larger than num2...")
Else
Response.Write("Num2 is larger than num1...")
End If
If statements can also be nested (put inside each other). An example
of this is:
If Num1 > Num2 Then
If num3 > num1 Then
Response.Write("Num3 is larger than num1 and num2...")
Else
Response.Write("Num3 is smaller than num1...")
End If
Else
If num3 > num2 Then
Response.Write("Num3 is larger than num2 and num1...")
Else
Response.Write("Num3 is smaller than num2...")
End If
End If
Sometimes it is necessary to check the value of one condition and then
check the value of another condition if the first one is met. This is
when you would want to use nested if statements. They allow you to check
multiple conditions. Just remember to include and "End If" with each
condition you test for. If you forget to include this the ASP Runtime
will give you an error.
Select Conditional Statements
Select statements are useful when you want to test one condition
and do multiple actions based upon it. The syntax of the select statement
is as follows:
Select Case myVarA
Case "1"
Response.Write("a=1")
Case "2"
Response.Write("a=2")
Case "3"
Response.Write("a=3")
Case "4"
Response.Write("a=4")
Case Else
Response.Write("a equals something else...")
End Select
What the above code does is check myVarA. If myVarA equals any of
the case statements the code under that condition is executed. If none
of the case statements match the variable then the code under the Case Else
condition is executed.
Check out Conditionals Part 2 for when to use if and when to use
the select case statement.
|
|
|
|