VBScript conditional block
VBScript conditional blocks execute groups of code based on a "condition" being true. The two conditional statements,
Select are explained here.
If conditional statement allows you to have the code executed "if" something has the result of
If Var1 = 1 Then Call SomeFunction()
The above line will execute the line
Call SomeFunction() if the
Var1 = 1 is True.
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
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 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 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
Thenis always required with an
- There must be one
If, zero or one
Elses and zero to as many as needed
- 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
Exitstatement with an
Ifblock like you can most other blocks.
- You should indent your code for readability based on the starting and ending of
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.
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
Case Elseis optional depending on what you need, but it has to be the last case.
- You can do funky stuff like
Select Case Trueand put variables as the
Cases and whichever variable is equal to
Truefirst will determine which block is executed.
- You cannot use the
Exitstatement with a
Selectblock like you can most other blocks.
- You can embed
Selectblocks inside other blocks.
- You should indent your code for readability based on the starting, ending, and