Datasheet
Chapter 1: A Quick Introduction to Programming
9
You can store several different types of data in variables. These are called data types and so far you’ve
seen two:
❑ String
❑ Integer
You’ve also seen a single-precision floating-point number in the tax rate example.
We’ll be covering all of them later on in the book. For now, just be aware that there are different data
types and that they can be stored in variables.
Flow Control
When you run a script that you have written, the code executes in a certain order. This order of execution
is also known as flow . In simple scripts such as the ones you looked at so far, the statements simply
execute from the top down. The script engine starts with the first statement in the script, executes it,
moves on to the next one, and then the next one, and so on until the script reaches the end. The execution
occurs this way because the simple programs you’ve written so far do not contain any branching or
looping code.
Branching
Take a look at a script that was used earlier.
Dim YourName
‘Above we initialized the variable
YourName = InputBox(“Hello! What is your name?”)
‘Above we ask for the user’s name and initialize the variable
MsgBox “Hello “ & YourName & “! Pleased to meet you.”
‘Above we display a greeting containing the user’s name
If you save this script in a file with a .vbs extension, and then execute it using the Windows Script Host,
all of the statements will be executed in order from the first statement to the last.
Note that it was previously mentioned that all of the statements will be executed. However, this isn’t
what you always want. There is a technique that you can use to cause some statements to be executed,
and some not, depending on certain conditions. This technique is called branching .
VBScript supports a few different branching constructs, and they are covered in detail in Chapter 5 , but
here we only cover the simplest and most common one, which is the
If...Else...End If construct.
Take a look at this modified code example.
c01.indd 9c01.indd 9 8/27/07 7:45:20 PM8/27/07 7:45:20 PM