Conditional statements allow you to control the flow of execution of a
script or one of its sections. To do this, you use some keywords and
associate them with expressions. Depending on the outcome of the
checking process and other comparison operations, you can take appropriate
actions.
|
Most of the conditional statements perform comparisons and act depending
on the outcome of such comparisons. To assist you in making such
comparisons, the VBScript language is equipped with special operators
that can act on natural numbers, decimal numbers, or strings.
|
We previously used the assignment operator = to give a value to a
variable. Although the assignment operator works on two operands, one on
the left and another on the right of the operator, it doesn't mean that
both operands are equal. It is only used to give a new value, the right
value, to the left value. Because of that, the left operand must never be
a numeric value, although both operands can be variables.
If you want to find out whether two variables hold the
same value, you should use the equality operator. This is
performed with = and its syntax is:
Variable1 = Variable2
To perform this operation, the browser (actually the
interpreter) compares the operands on both sides of the = operator. If both
operands hold the same value, the comparison renders a value of true.
Otherwise, the comparison renders false. |
To compare two variables in order to find out whether they are different,
you can use the inequality operator <> whose syntax is:
Variable1 <> Variable2
When performing this operation, the browser compares
the values of Variable1 and Variable2. If their values are different,
which means that they are not equal, the comparison results in a true
value (very important to understand). If they are equal, the result of the
comparison is false (observe the contrast with the equality operator =).
|
If you want to find out whether one value is less than another, use the
"less than" operator <. Its syntax is:
Variable1 < Variable2
The browser compares the values held by Variable1 and
Variable2. If the value held by Variable1 is less than that of Variable2,
the comparison would produce a true value. Otherwise, the result is
rendered false. |
Decision Makers: The If...Then Statement |
|
The If...Then statement examines
the truthfulness of an expression. Structurally, its formula is:
If ConditionIsTrue
Then Statement
Therefore, the program will examine a Condition.
This condition can be a simple expression or a combination of
expressions. If the Condition is true, then the program will execute
the Statement.
There are two ways you can use the If...Then
statement. If the conditional formula is short enough, you can write
it on one line, like this:
If Condition
Then Statement
If there are many statements to execute as a
truthful result of the condition, you should write the statements on
alternate lines. Of course, you can use this technique even if the
condition you are examining is short. In this case, one very
important rule to keep is to terminate the conditional statement
with End If. Here is an example:
If Condition
Then
Statement
End If
Here is another example:
If Condition
Then
Statement1
Statement2
Statementn
End If
|
|
|