![]() |
Table of Contents |
![]() |
Conditional ExecutionMany administrative scripts that you write will execute some simple, straightforward task that doesn't require any decisions. Other scripts, however, will be more complex, and will require your scripts to evaluate conditions and values and make a decision about what to do. VBScript conditional execution statements make this possible, giving your scripts a form of intelligence and decision-making capabilities. If…ThenThe most common conditional execution statement is the If…Then construct. It's referred to as a construct because it involves more than a single statement or more than even a single line of code. Here's a very simple example. Dim iMyNumber iMyNumber = InputBox("Enter a number from 1-100") If iMyNumber < 1 Or iMyNumber > 100 Then MsgBox "I said 1 to 100!" Else MsgBox "Thank you!" End If The script declares a variable named iMyNumber, and then uses InputBox() to retrieve user input. Next, the script uses an If…Then construct to evaluate the input. Here's how it works.
Let's walk through what happens if you run this script and enter the number 2 in the input box.
Now, let's look at what happens if you enter 101 in the input box instead.
TIP In the next example, I slightly indented the lines of code within each section of the If…Then construct. This indenting makes it easier to visually spot which code will execute in either condition. Nesting If…ThenIf…Then constructs can be nested as well, meaning you can place them one within the other. Let's extend the sample script to be a bit more complex. Dim iMyNumber iMyNumber = InputBox("Enter a number from 1-100") If iMyNumber < 1 Or iMyNumber > 100 Then If iMyNumber > 10000 Then MsgBox "You're not being serious!" End If MsgBox "I said 1 to 100!" Else MsgBox "Thank you!" End If I didn't change anything after the Else statement, but I did add another If…Then construct after the Then statement. Here's what will happen if you run this script and enter 20,000 in the input box.
TIP Note the indenting in the following sample. All of the code within each construct is indented. When you start nesting constructs, indenting can help make sure you're matching up If and End If statements correctly. Also, notice that the second If…Then construct doesn't include an Else statement. Else is always optional, and you don't have to include it. The only required statements are If, Then, and End If. If…Then…Else…ElseIfWhat if you want to evaluate multiple, different, possible values in a single If…Then construct? You can, using ElseIf. I'll revise the last sample to show you how it works. Dim iMyNumber iMyNumber = InputBox("Enter a number from 1-100") If iMyNumber < 1 Then MsgBox "That isn't more than 1" ElseIf iMyNumber > 100 Then MsgBox "That isn't less than 100" Else MsgBox "Thank you!" End If Here's how VBScripts treats that code.
You can stack up any number of ElseIf statements to evaluate various conditions. Listing 10.1 is an over-the-top example to give you the idea. Listing 10.1. ElseIf.vbs. Using ElseIf.Dim iMyNumber iMyNumber = InputBox("Enter a number from 1-100") If iMyNumber = 1 Then MsgBox "1 is a good number." ElseIf iMyNumber > 1 And iMyNumber < 50 MsgBox "2 to 49: Numbers of indecision" ElseIf iMyNumber = 50 MsgBox "Heading right for the middle, huh?" ElseIf iMyNumber > 50 And iMyNumber < 99 MsgBox "51 to 99: You like the upper half" ElseIf iMyNumber = 99 MsgBox "99 is just short of 100" ElseIf iMyNumber = 100 MsgBox "You went all the way!" Else MsgBox "You didn't enter 1 to 100!" End If Perhaps not an overly exciting example, but this definitely shows how ElseIf can allow your scripts to react to very specific conditions and execute different lines of code for each. Select…CaseIf you've mastered the use of ElseIf, you'll really appreciate the Select…Case construct. Listing 10.2 is the sample from Listing 10.1, rewritten to use Select…Case. Listing 10.2. SelectCase.vbs. Using Select…Case.Dim iMyNumber iMyNumber = InputBox("Enter a number from 1-100") Select Case iMyNumber Case 1 MsgBox "1 is a good number." Case 2 To 49 MsgBox "2 to 49: Numbers of indecision" Case 50 MsgBox "Heading right for the middle, huh?" Case 51 To 98 MsgBox "51 to 99: You like the upper half" Case 99 MsgBox "99 is just short of 100" Case 100 MsgBox "You went all the way!" Case Else MsgBox "You didn't enter 1 to 100!" End Select This script will produce the same results as the one in Listing 10.1, but it uses a more streamlined syntax to evaluate conditions for multiple values. The Select...Case statement specifies what's going to be evaluated; in this case, it's the variable iMyNumber. Each subsequent Case statement provides a specific evaluation: equal to 1, from 2 to 49, equal to 50, from 51 to 98, equal to 99, or equal to 100. If any one of these evaluations is true, VBScript executes the appropriate code and then heads straight to the closing End Select statement. If none of the Case expressions evaluate to True, VBScript executes whatever it finds with Case Else. As with the Else statement in an If…Then construct, Case Else is optional. If you omit it and none of your Case expressions are True, VBScript will just start executing whatever code follows End Select. |
![]() |
Table of Contents |
![]() |