Use the If...Then...Else statement if you want to
- execute some code if a condition is true
- select one of two blocks of code to execute
If you want to execute only one statement when a condition is true, you can write the code on one line:
If i=10 Then response.write("Hello")There is no ..Else.. in this syntax. You just tell the code to perform one action if a condition is true (in this case If i=10).
If you want to execute more than one statement when a condition is true, you must put each statement on separate lines, and end the statement with the keyword "End If":
If i=10 Thenresponse.write("Hello")
i = i+1
End If
There is no ..Else.. in the example above either. You just tell the code to perform multiple actions if the condition is true.
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:
Example
If i < 10 Then
response.write("Good morning!")
Else
response.write("Have a nice day!")
End If
In the example above, 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 10).
Practice Excercise Practice now