May 13, 2021 VBScript
Conditional statements are used to perform different operations depending on the situation.
In VBScript, we can use four conditional statements:
In the following cases, you can use If... T hen... Else statement:
If you execute only one statement when the condition is true, you can write the code as one line:
In the code above, there is no .. E lse.. S tatement. W e only have the code perform an action when the condition is true (when i is 10).
If more than one statement is executed when the condition is true, you must write a statement on one line and then end the statement with the keyword "End If":
In the code above, there is also no .. E lse.. S tatement. We only let the code do more than one operation when the condition is true.
If you want to execute a statement when the condition is true and another statement when the condition is not true, you must add the keyword "Else":
In the code above, the first piece of code is executed when the condition is true, and the second piece of code is executed when the condition is not true (when i is greater than 10).
If you want to select one of several sets of code to execute, you can use If... T hen... ElseIf statement:
If you want to select one of several sets of code to execute, you can use the Select Case statement:
How the above code works: First, we need a simple expression (often a variable), and this expression is evaluated once. T he value of the expression is then compared to the value in each Case. If matched, the code for the matched Case is executed.
In this section, you've learned about VBScript conditional statements, and in the next section, you'll touch VBScript loop statements, which are often used together!