ASP Article
Conditional Statements
Conditional statements are used to perform different actions for different decisions.
In ASP there are four conditional statements:
- If...Then statement - executes a set of code when a condition is true
- If...Then...Else statement - select one of two sets of lines to execute
- If...Then...ElseIf statement - select one of many sets of lines to execute
- Select Case statement - select one of many sets of lines to execute
If...Then
Use the If...Then statement to execute some code if a condition is true.
You just tell the code to perform one action if a condition is true (in this case If i=10).
Dim i: i=10
If i = 10 Then response.write("Hello")
OUTPUT:
Hello
If you want to execute
You just tell the code to perform multiple actions if the condition is true like the example below.
Here the two actions are write Hello and incrementing i by 1
If i=10 Then
response.write("Hello")
i = i+1
End If
If you want to execute a statement if a condition is true and execute another statement if the condition is not true, you must add the Else keyword:
In the example below, the first block of code will be executed if the condition is true, and the other block will be executed otherwise (if i is greater than or equal to 10).
i=hour(time)
If i < 10 Then
response.write("Good morning!")
Else
response.write("Have a nice day!")
End If
OUTPUT if less than 10;
Good morning!
OUTPUT if 10 or greater:
Have a nice day!
If...Then...ElseIf
You can use the If...Then...ElseIf statement if you want to select one of many blocks of code to execute:
i=hour(time)
If i = 10 Then
response.write("Just started...!")
ElseIf i = 11 Then
response.write("Hungry!")
ElseIf i = 12 Then
response.write("Ah, lunch-time!")
ElseIf i = 16 Then
response.write("Time to go home!")
Else
response.write("Unknown")
End If
OUTPUT if i = 10:
Just started...!
OUTPUT if i = 11:
Hungry!
OUTPUT if i = 12:
Ah, lunch-time!
OUTPUT if i = 16:
Time to go home!
OUTPUT if none of the above:
Unknown
Select Case
You can also use the Select Case statement if you want to select one of many blocks of code to execute:
This is how it works: First we have a single expression (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each Case in the structure. If there is a match, the block of code associated with that Case is executed.
d=weekday(date)
Select Case d
Case 1
response.write("Sleepy Sunday")
Case 2
response.write("Monday again!")
Case 3
response.write("Just Tuesday!")
Case 4
response.write("Wednesday!")
Case 5
response.write("Thursday...")
Case 6
response.write("Finally Friday!")
Case else
response.write("Super Saturday!!!!")
End Select
OUTPUT case 1:
Sleepy Sunday
OUTPUT case 2:
Monday again!
OUTPUT case 3:
Just Tuesday!
OUTPUT case 4:
Wednesday!
OUTPUT case 5:
Thursday...
OUTPUT case 6:
Finally Friday!
OUTPUT case 7:
Super Saturday!!!
Loops and Conditionals are the heart of PHP and any code and is what makes it all tick! Happy Coding!