|Lessons||VBScript If Statement|
VBScript If statement is used to make decisions in our code depending upon the result of some condition.
For example we want to greet the visitors of our website with appropriate messages; good morning, if it is morning, good evening if it is evening, etc.Here is the syntax of the If statement:
If [condition] Then
' VBScript code to execute if the condition is true
We write the keyword If followed by a condition. A condition is any VBScript expression that evaluates to true or false. A condition is usually written by using one of the several comparison operators available in VBScript. After the condition we write the keyword Then and then we write the code that we wish to be executed in case the condition turns out to be true. Finally we write the words End If to end the If code block.
Here is an example:
a = 10
b = 20
If a < b Then
document.write("A is less than B")
Here is the output:
A is less than B
The If statement in the above code checks if the value of the variable a is less than b and then outputs the text "A is less than B", if the condition is met.
VBScript Comparison Operators
Following table lists the comparison operators available in VBScript:
Next >>> Lesson No. 9: VBScript If Else and Elseif