Datasheet

Chapter 1: A Quick Introduction to Programming
11
3. What happens if the user does (as you expect) type something into the input box? Well, this is
where the next line comes in.
Else
You can actually begin to read the code and in fact doing this helps it to make sense. What the
whole loop actually means is that if the value of variable
YourName is a zero-length string, then
assign the variable
Greeting with one value; however, if it contains something else, do some-
thing else (assign
Greeting a different value). This doesn’t protect your script from users enter-
ing data like numbers of non-alphabet characters into the test box, although you could code for
all these conditions if you wanted to.
4. The final line of the code uses the MsgBox function to display the value of the variable
Greeting . Notice that both lines of code assign a value to the Greeting variable. However,
only one of these lines will actually execute in any one running of the script. This is because the
If...Else...End If block makes an either/or decision. Either a given condition is True , or
it is
False . There’s no way it can be neither (not a string that contains text nor a zero-length
string) or both (a zero-length string that contains text). If it is
True , then the script engine will
execute the code between the
If and Else statements. If it is False , then it will execute the code
between the
Else and End If statements.
So, what the complete script does is test the input, and then executes different code, depending on the
result of that test, and hence the term branching. Using this technique allows your script to adapt to the
unpredictable nature of the input. Compare the intelligent script to the following one, which looks pretty
lame.
Dim YourName
Dim Greeting
YourName = InputBox(“Hello! What is your name?”)
Greeting = “Hello, “& YourName & “, great to meet you.”
MsgBox Greeting
This script is just plain dumb because it does not contain any branching logic to test the input; so when
the user does something unpredictable, such as clicking the
Cancel button, or not entering any name at
all, the script does not have the ability to adapt. Compare this to your intelligent script, which is capable
of adapting to the unpredictability of input by testing it with
If...Else...End If branching.
Figure 1-6
c01.indd 11c01.indd 11 8/27/07 7:45:21 PM8/27/07 7:45:21 PM