Difference between revisions of "VBScript conditional block"
(→Select Case block) |
m (1 revision imported) |
(No difference)
|
Latest revision as of 08:20, 1 December 2017
VBScript conditional blocks execute groups of code based on a "condition" being true. The two conditional statements, If
and Select
are explained here.
Contents
If statement
An If
conditional statement allows you to have the code executed "if" something has the result of True
.
If Var1 = 1 Then Call SomeFunction()
The above line will execute the line Call SomeFunction()
if the Var1 = 1
is True.
If block
What if you want multiple lines to execute? To do that, the other way is the following (more common):
If Var1 = 1 Then AddQ "Var1 equals 1." Disconnect End If
In this example, the AddQ function and the Disconnect function will be called when the if block executes.
Note that if the condition is false, the lines inside will be skipped over, but they must be valid VBScript (use comments if you want to hide something from the parser).
Else statement
Add an Else
statement and something will always happen if the expression happens to be false:
If Var1 = 1 Then AddQ "Var1 equals 1." Else AddQ "Var1 does not equal 1." End If
If blocks can also be embedded:
If Var1 = 1 Then AddQ "Var1 equals 1." Else If Var1 = 2 Then AddQ "Var1 equals 2." Else AddQ "Var1 does not equal 1 or 2." End If End If
ElseIf statement
Using the ElseIf
keyword, the above can be combined:
If Var1 = 1 Then AddQ "Var1 equals 1." ElseIf Var1 = 2 Then AddQ "Var1 equals 2." Else AddQ "Var1 does not equal 1 or 2." End If
Notes
Then
is always required with anIf
or anElseIf
.- There must be one
If
, zero or oneElse
s and zero to as many as neededElseIf
s. - There is an If...Then...Else statement that can go on one line, but it is not very readable thus it is avoided.
- You cannot use the
Exit
statement with anIf
block like you can most other blocks. - You should indent your code for readability based on the starting and ending of
If
blocks.
Select block
A Select
block allows you to choose the course of action based on a value. It is basically a different structure to an If...ElseIf...Else block specifically made for when the same value is being compared repeatedly.
Syntax:
Select Case (VARIABLE) Case (VALUE) ' when (VARIABLE) = (VALUE) (STATEMENTS) Case (VALUE2) ' when (VARIABLE) = (VALUE2) (STATEMENTS) Case Else ' when (VARIABLE) is not any of the above (STATEMENTS) End Select
The above If...ElseIf...Else block can be changed to:
Select Case Var1 Case 1 AddQ "Var1 equals 1." Case 2 AddQ "Var1 equals 2." Case Else AddQ "Var1 does not equal 1 or 2." End Select
Notes
Case Else
is optional depending on what you need, but it has to be the last case.- You can do funky stuff like
Select Case True
and put variables as theCase
s and whichever variable is equal toTrue
first will determine which block is executed. - You cannot use the
Exit
statement with aSelect
block like you can most other blocks. - You can embed
Select
blocks inside other blocks. - You should indent your code for readability based on the starting, ending, and
Case
statements ofSelect
blocks.