Export (0) Print
Expand All

Make a Decision

By The Microsoft Scripting Guys

Sesame Script

Welcome to Sesame Script, the column for beginning script writers. The goal of this column is to teach the very basics of Windows scripting for system administration automation. We’ll provide you with the information you’ll need to begin reading and understanding scripts and to start modifying those scripts to suit your own needs. If there’s anything in particular about scripting you’re finding confusing, let us know; you’re probably not alone.

Check the Sesame Script Archives to see past articles.

On This Page

Make a Decision
If…Then
If…Then…Else
If…Then…ElseIf…Else
Keeping Order

Make a Decision

You probably have to make thousands of decisions every day: Should I get out of bed now or sleep for another 10 minutes? What should I eat for breakfast? Do I really have to go to work today? Do I want to wear the red shirt or the blue shirt? Do I really have to go to work today? (That one does come up a lot doesn’t it?)

Of course, there are always consequences to these decisions. If you sleep for another 10 minutes you might be late for work. If you eat donuts for breakfast you might have to spend an extra 10 minutes on the treadmill. If you don’t go to work you might lose your job. We’re not sure what the consequences of wearing the red shirt rather than the blue shirt would be.

If…Then

Let’s take a look at our first choice of the day: should I sleep for 10 more minutes? If you were to script this decision, it might look something like this:



If I Sleep For Ten More Minutes Then
    I’ll be late for work
End If

Take a look at the syntax of this statement. We start with the word If. This is where our decision begins: if a certain event is true…. The next thing we have to specify is what that event is. In this case we’re saying that it is sleeping longer. If it’s true that you’re going to sleep for 10 more minutes, then something will happen. Notice the Then keyword at the end of the first line. In the first line of our sleeping script we’ve stated If it’s true that we sleep for 10 more minutes Then. Then what?

That’s where the next line comes in. Here we say what happens if we sleep in; in this case, we’ll be late for work. If more will happen than just being late for work, we can have more than one line here:



If I Sleep For Ten More Minutes Then
    I’ll be late for work
    My boss will be very angry
End If

So here, If it’s true that we sleep in Then not only will we be late, but our boss will be pretty upset about it. After all the results of our oversleeping have been listed, we put an end to the list of “what happens” with an End If statement.

And we’ve just put together our first If Then statement. No, you can’t actually put our If Then sleeping statement into a VBScript script and run it, but you can use the exact same process to put together a statement that you can use in your script. For example, let’s say you have two numbers stored in the variables A and B. (Don’t know what variables are? Take a look at the Sesame Script on Variables and Constants.) If it’s true that the value of A is greater then the value of B, you want to echo A. After our sleeping statement, this is a breeze, right?

You were supposed to say “right, this is a breeze.” That’s because it is:



A = 5
B = 10

If A > B Then
    Wscript.Echo A
End If

All we did here was assign values to A and B, then we put in our If Then statement. We started with the If keyword, of course. If what? Well, we want to know if it’s true that A is greater than B, so we simply put the comparison A > B into our statement:



If A > B

We next add our Then keyword:



If A > B Then

We’re now back to the “what happens” part of the statement. If A is greater than B Then what? Well, then we want to echo A:



If A > B Then
    Wscript.Echo A

That’s all we really want to do if A is greater than B, so we can close out our If Then statement with the End If keywords:



If A > B Then
    Wscript.Echo A
End If

That’s all there is to it. See, we told you it was a breeze.

If…Then…Else

Let’s go back to our sleeping example again. We’ve figured out what will happen if we oversleep, but will anything happen if we don’t oversleep? Well, if we don’t oversleep we’ll most likely get to work on time. How would we put that into our If Then statement? By adding an Else:



If I Sleep For Ten More Minutes Then
    I’ll be late for work
    My boss will by very angry
Else
    I’ll be on time for work
End If

We start out with our If keyword, followed by the condition we want to identify as being True or False, and add our Then keyword. We then put in the statements that will happen if our If condition is True, in other words, if we really do oversleep. But this time, instead of closing out our If statement with an End If, we put in the keyword Else. We want to specify what will happen if our condition is not true, if we don’t oversleep. That’s what the Else keyword is for. We follow the Else keyword with the statements that occur if the If condition is False. And what happens if we don’t oversleep? Well, this does:



Else
    I’ll be on time for work

That’s right, get up on time and we’ll probably make it to work on time. In addition, just like with the statements that follow If Then, we can specify more than one statement to follow the Else statement:



Else
    I’ll be on time for work
    My boss will be happy

When we’ve entered all the statements that will occur following our Else we once again close out the entire statement with an End If. Let’s take one more look at our If…Then…Else statement:



If I Sleep For Ten More Minutes Then
    I’ll be late for work
    My boss will be very angry
Else
    I’ll be on time for work
    My boss will be happy
End If

Following along with this, if we do oversleep the statement “I Sleep For Ten More Minutes” is True, which means we’ll be late for work and our boss will be angry. If it’s False, we don’t oversleep, we’ll be on time for work, and we’ll have a happy boss. (All right, maybe the boss still won’t be happy, but at least it won’t be because we’re late.)

Now let’s turn this into a real scripting example. Again we’re going to compare the value of A to the value of B. But this time, if A is not greater than B we want to echo the value of B. All right, this time we’re sure you can figure it out. But we’ll show it to you anyway:



A = 5
B = 10

If A > B Then
    Wscript.Echo A
Else
    Wscript.Echo B
End If

Here we’ve assigned values to the variables A and B, then check to see If A is greater than B. If it is - if the statement A > B is True - we echo the value of A. If the statement A > B is False, we jump down to the Else statement and echo the value of B. Put this statement into a script and run the script and your output will be 10, because A (5) is not greater than B (10).

If…Then…ElseIf…Else

Suppose sleeping in for 10 minutes isn’t really going to make us late for work. Maybe if we skip breakfast (which the Scripting Guys would never recommend) or don’t brush our teeth (which the Scripting Guys and the dentist would never recommend) we can still make it on time. However, if we get up any later than that, more than 10 minutes late, we’ll never make it to work on time. We now have more than just a single True/False condition, we have three conditions: We oversleep by 10 minutes or less; we oversleep by more than 10 minutes; or we don’t oversleep at all. Can we account for all these conditions with one If statement? We can if we add an ElseIf:



If I Sleep For An Extra Ten Minutes Or Less Then
    I’ll have to skip breakfast
    I’ll be on time for work
ElseIf I Sleep For An Extra 11 Minutes Or More Then
    I’ll be late for work
Else
    I’ll be on time for work
    My boss will be happy
End If

We once again start with If, followed by our condition. We’ve changed our condition a little bit to fit the new scenario; this time we’re checking to see whether we’ve overslept by ten minutes or less. We put in our Then keyword, and then we can put in the statements that will occur if our condition is true (in this case we’ll have to skip breakfast but we’ll be on time for work).

Next we’ve added a new keyword, the ElseIf keyword. Notice there’s no space between Else and If, it’s all one word: ElseIf. The ElseIf keyword allows us to check a second condition. Here we’re checking to see if it’s true that we’ve overslept by 11 minutes or more. Because we’ve specified another condition we once again have to follow the condition with the Then keyword: ElseIf condition Then. If this condition is true, the statements that follow will occur: we’ll be late for work. We then continue on with our Else statement and close it all out with the End If statement. Note that you don’t have to have an Else statement, you can simply have an If and an ElseIf (ending of course with the End If). And, as with the others, you can have multiple statements following the ElseIf condition:



If I Sleep For An Extra Ten Minutes Or Less Then
    I’ll have to skip breakfast
    I’ll be on time for work
ElseIf I Sleep For An Extra 11 Minutes Or More Then
    I’ll be late for work
    My boss will be very angry
Else
    I’ll be on time for work
    My boss will be happy
End If

You know what comes next; it’s time to create real script code from this. Let’s take a look at our last statements:



A = 5
B = 10

If A > B Then
    Wscript.Echo A
Else
    Wscript.Echo B
End If

We know what will happen if A is greater than B (we’ll echo A), and we know what will happen if it’s not (we’ll echo B). But if you think about it, if A is not greater than B there are actually two possibilities left: A is equal to B, and A is less than B. We’ve decided we want to distinguish between the two and echo different statements based on the results. Let’s echo back some strings to make it clear what happened:



A = 5
B = 10

If A > B Then
    Wscript.Echo "A is greater than B"
ElseIf A = B Then
    Wscript.Echo "A is equal to B"
Else
    Wscript.Echo "A is less than B"
End If

After assigning values to A and B we begin our If statement with the If keyword, the condition we’re testing for True (A > B), followed by the Then keyword. After that we put in the statement that will run if the condition is true, if A is greater than B:



If A > B Then
    Wscript.Echo "A is greater than B"

Next comes our ElseIf statement. We start with the ElseIf keyword followed by the condition we’re testing (A = B) and ending with the Then keyword. We then put in the statement we want to run if the condition is True, if A is equal to B:



ElseIf A = B Then
    Wscript.Echo "A is equal to B"

Finally, if neither of the conditions is True - if A > B is False and A = B is False - we’ll fall into our Else keyword and any following statements will run:



Else
    Wscript.Echo "A is less than B"

And of course end it all with an End If.

More Information

Microsoft Windows 2000 Scripting Guide - If Then ElseIf

Keeping Order

Before we go, we’d like to show you one more If…Then…ElseIf…Else statement:



A = 5
B = 10
C = 15

If A < C Then
    Wscript.Echo "A is less than 15"
ElseIf A < B Then
    Wscript.Echo "A is less than 10"
Else
    Wscript.Echo "A is greater than 15"
End If

There are a couple of problems with this script. The most obvious is probably the fact that there is no condition that covers what would happen if A were equal to 15. You can see from the messages that the results of this script will state that A is either less than 15, less than 10, or greater than 15. What happens if it’s equal to 15? Well, let’s walk through and take a look.

In the If statement, if A is less than C then the message “A is less than 15” will be echoed:



If A < C Then
    Wscript.Echo "A is less than 15"

The next condition tests to see whether A is less than B. If it is, the message “A is less than B” will be echoed:



ElseIf A < B Then
    Wscript.Echo "A is less than 10"

If both of these conditions are false (that is, if A is not less than C and A not less than B) the message “A is greater than B” is echoed:



Else
    Wscript.Echo "A is greater than 15"

That means that if A is equal to 15, we’ll first check to see if A is less than C, which contains a value of 15. Well, 15 is not less than 15, so we’ll move on to the ElseIf statement. Now we check if A is less than B, which contains a value of 10. 15 is not less than 10, so that condition is false, and once again we move on, this time to the Else statement. Since all previous conditions are false we run the statement following the Else statement, which means that if A is equal to 15 we’ll echo the message “A is greater than 15.” Obviously this isn’t correct.

The second problem with this statement is less obvious. Let’s take another look at our script with our original values:



A = 5
B = 10
C = 15

If A < C Then
    Wscript.Echo "A is less than 15"
ElseIf A < B Then
    Wscript.Echo "A is less than 10"
Else
    Wscript.Echo "A is greater than 15"
End If

What’s going to happen when this script runs? Well the first thing that will happen is that the condition A < C will be tested. If that condition is True the message “A is less than 15” will be echoed. A has a value of 5 and C has a value of 15, so A < C will be True, meaning we’ll echo “A is less than 15.” At this point a True condition has been met and the statements have been executed, so the script will end. The ElseIf and Else statements will never be checked. The problem with this? Take another look at the ElseIf:



ElseIf A < B Then
    Wscript.Echo "A is less than 10"

With A having a value of 5 and B having a value of 10, the conditional statement in the ElseIf, A < B, is True. But because the conditional statement in the If statement was also True, we never get to this statement to find out that not only is A less than 15, but it’s even less than 10. This makes the ElseIf statement useless because we’ll never reach it, and we’ll never know whether A is less than 10, we’ll know only whether it’s less than 15.

How would we solve this problem? It’s actually pretty simple: just change the order of the conditional statements. If we check for A less than 10 before we check for A less than 15, we’ll catch both conditions:



A = 5
B = 10
C = 15

If A < B Then
    Wscript.Echo "A is less than 10"
ElseIf A < C Then
    Wscript.Echo "A is less than 15"
Else
    Wscript.Echo "A is greater than 15"
End If

This time we check the condition A < B. A, with a value of 5, is less than B, with a value of 10, so the message “A is less than 10” will be echoed back. The script still ends and we don’t check the next condition, but that’s okay: if A is less than 10 it’s a pretty safe bet that it’s also less than 15. Therefore, we don’t actually need to see that message. But if we change A to a value of 11, then what happens? We’ll test to see if A < B, but this time it isn’t; 11 is not less than 10. So this time we fall into the ElseIf statement and see if A < C is true. And because 11 is less than 15, we’ll echo back “A is less than 15” and the script will end.

Much better.

And tomorrow we get to start all over again… Do I really have to go to work today?

Test out your newfound knowledge of If statements by solving this puzzle: 101 Scripting Abominations.


Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft