VBScript Conditional Statements
Conditional Statements
Conditional statements are used to perform different actions based on different conditions.
In VBScript, we can use four types of conditional statements:
If Statement - Use this statement if you want to execute a series of code when the condition is true.
If...Then...Else Statement - Use this statement if you want to execute one of two sets of code.
If...Then...ElseIf Statement - Use this statement if you want to choose one of multiple sets of code to execute.
Select Case Statement - Use this statement if you want to choose one of multiple sets of code to execute.
If...Then...Else
You can use the If...Then...Else statement in the following cases:
To execute a piece of code when the condition is true.
To choose one of two pieces of code to execute.
If you only need to execute one statement when the condition is true, you can write the code in one line:
In the above code, there is no ..Else.. statement. We simply make the code execute one action when the condition is true (when i=10).
If you need to execute more than one statement when the condition is true, you must write each statement on a new line and use the keyword "End If" to end the statement:
In the above code, there is also no ..Else.. statement. We simply make the code execute multiple actions 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":
Example (IE only)
In the above code, the first block of code will be executed when the condition is true, and the second block of code will be executed when the condition is not true (when i is greater than 10).
If...Then...ElseIf
If you want to choose one of multiple sets of code to execute, you can use the If...Then...ElseIf statement:
Example (IE only)
Select Case
If you want to choose one of multiple sets of code to execute, you can use the "Select Case" statement:
Example (IE only)
The above code works as follows: First, we need a simple expression (often a variable) that will be evaluated once. Then, the value of the expression is compared with the value in each Case. If a match is found, the code corresponding to that Case is executed.