Conditional Statements in VBScript
(Page 1 of 4 )
Inevitably, as you progress to more complex scripts, your scripts will need to be able to make decisions and take separate actions based upon those conditions. For this, VBScript provides conditional statements.
Conditional statements route code execution based upon the value of some expression. These conditions typically involve some comparison and are evaluated to either True or False. The conditional statement then branches off code execution accordingly.
Imagine the following scenario:
You're writing a script to open or close a door. Which action should be performed is strictly dependent upon the current state of the door. In other words, you can't open a door that's already open! So some code might look like this.
Set objDoor = CreateObject("My.Door")
If objDoor.IsOpen Then
objDoor.Close
Else
objDoor.Open
End If
In this hypothetical example we have an object name objDoor that represents our door. This object class has Open and Close methods to perform our action as well as an IsOpen property that returns a value of True if the door is currently open.
I've made use of an IF statement to check the value of the IsOpen property. If it evaluates to True the immediate code block is executed. If it evaluates to False, the Else block is executed. Code would then continue with the line immediately following the End If statement.
Of course, this is an extremely simplified example. The If statement offers some more advanced flexibility and there are several ways to construct much more intricate expressions. To begin, let's take a look at the If statement up close.
Next: The IF statement >>
More BrainDump Articles
More By Nilpo