Previous Section Table of Contents Next Section

Conditional Execution

Many 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…Then

The 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.

  • First, VBScript evaluates the two logical expressions in the If statement. Does iMyNumber contain a number that is less than one? Does it contain a number that is more than 100? If either of these two conditions are true, VBScript will execute the code following the Then statement. VBScript will accept either of these two conditions because they're connected with an Or statement, which means either one of them being true is acceptable.

  • If neither of the If conditions are true, VBScript looks for an alternate execution path, which it finds after the Else statement. VBScript executes that code instead of the code following Then.

  • Conditional execution stops whenever another portion of the If…Then construct is reached.

Boolean Operators

And and Or are examples of Boolean operators. These operators are similar to mathematical operators, except that instead of resolving a value, these resolve a logical condition and return a True or False value.

For example, suppose you have a variable named iNumber, which contains the value 4. The logical condition iNumber > 1 And iNumber < 100 would evaluate to True, because both subconditions evaluate to True. Similarly, the logical condition iNumber > 1 Or iNumber < 0 would also evaluate to True, because one of the two subconditions evaluates to True.

On the other hand, iNumber > 1 And iNumber > 100 would evaluate to False, because only one of the two subconditions evaluates to True. The rules regarding And and Or are pretty simple: With And, both conditions must be true in order for the overall evaluation to be true. With Or, either or both conditions must be true in order for the overall expression to be evaluated as true.

You can get complex with Boolean operators, and you can group them with parentheses to control the order of evaluation. Consider this monster example: (iNumber > 10 Or iNumber < 5) And (iNumber <> 5 And iNumber <> 10). How will this evaluate?

First, VBScript looks at the deepest level of parentheses and evaluates them left to right for True or False. The first expression is iNumber > 10 Or iNumber < 5. Because iNumber is less than five, this expression evaluates as True. VBScript now looks at iNumber <>5 And iNumber <> 10. This expression is also True, because iNumber is neither 5 nor 10. Now, VBScript evaluates the last expression, which comes down to True And True. The result of this is True, so the overall expression's result is True.

What would this evaluate to if iNumber contained ten? It would be False. The first expression in parentheses is False, because iNumber is neither greater than ten nor less than five. The second expression is also False, because iNumber does equal ten. The final result becomes False And False, which is False.


Let's walk through what happens if you run this script and enter the number 2 in the input box.

  1. VBScript evaluates the If conditions and discovers that iMyNumber is neither less than one nor more than 100. VBScript looks for an alternative, which it finds in the Else statement.

  2. VBScript executes all code following the Else statement, displaying a message box reading "Thank you!"

  3. VBScript encounters the End If statement, meaning the If…Then construct is complete. VBScript begins executing any code that follows End If.

Now, let's look at what happens if you enter 101 in the input box instead.

  1. VBScript evaluates the If conditions. The first condition isn't true, but the second one is. Because the conditions are connected by an Or statement (as opposed to an And statement, which would require them both to be true), VBScript resolves the overall If statement as true, and begins executing everything that follows Then.

  2. VBScript displays a message box that reads, "I said 1 to 100!"

  3. VBScript encounters the Else statement. This tells VBScript that the current block of code is complete, and it looks for the End If statement.

  4. VBScript locates End If and begins executing any code that follows it.

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…Then

If…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.

  1. VBScript will evaluate the If conditions and find that iMyNumber is indeed greater than 100, forcing execution of the code following Then.

  2. VBScript will evaluate the second If…Then construct. Because it's true, VBScript will display a message box that reads, "You're not being serious!"

  3. VBScript will continue to execute the original Then code, displaying a message box that reads, "I said 1 to 100!"

  4. Finally, VBScript will hit the Else statement, telling it to jump right to End If.

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…ElseIf

What 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.

  1. The first If expression is evaluated. If it's true, VBScript executes everything following Then.

  2. If the first If expression is false, VBScript evaluates the ElseIf expression. If that's true, it executes whatever follows Then.

  3. If the ElseIf expression is false, VBScript executes whatever is after Else.

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…Case

If 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.

    Previous Section Table of Contents Next Section