24
Part
I
Getting Started with Windows Scripting
Comparison operators
When you perform comparisons, you check for certain conditions, such as whether A is greater than
B, or if A is equal to C. You primarily use comparison operators with conditional statements, such as
If Then
and
If Then Else
. The available operators are summarized in Table 2-7.
TABLE 2-7
Comparison Operators in VBScript
Operator
Description
=
Equality; evaluates to True if the values are equal.
<>
Inequality; evaluates to True if the values are not equal.
<
Less than; evaluates to True if value1 is less than value2.
<=
Less than or equal to; evaluates to True if value1 is less than or equal to value2.
>
Greater than; evaluates to True if value1 is greater than value2.
>=
Greater than or equal to; evaluates to True if value1 is greater than or equal to value2.
Listing 2-3 shows how you can use comparison operators in a script. Note that you can use these
operators to compare numbers as well as strings and that there is no set precedence order for com-
parisons. Comparisons are always performed from left to right.
LISTING 2-3
Scripting with Comparison Operators
checktotal.vbs
currTotal = 519
prevTotal = 321
if currTotal = 0 Then
WScript.Echo “The total is zero.”
End If
if currTotal = prevTotal Then
WScript.Echo “The totals are
equal.”
End If
if currTotal <> 0 Then
WScript.Echo “The total does NOT
equal zero.”
End If
if currTotal <> prevTotal Then
86804c02.indd 24
86804c02.indd 24
1/21/09 1:24:29 PM
1/21/09 1:24:29 PM
25
VBScript
Essentials
2
WScript.Echo “The totals are NOT
equal.”
End If
if currTotal < 0 Then
WScript.Echo “The total is less
than zero.”
End If
if currTotal > 0 Then
WScript.Echo “The total is
greater than zero.”
End If
if currTotal <= prevTotal Then
WScript.Echo “currTotal is less
than or equal to prevTotal.”
End If
if currTotal >= 0 Then
WScript.Echo “The total is
greater than or equal to zero.”
End If
Output
The total does NOT equal zero.
The totals are NOT equal.
The total is greater than zero.
The total is greater than or equal
One other comparison operator you should learn about is the special operator
Is
. You use
Is
to
compare objects, such as buttons. If the objects are of the same type, the result of the comparison is
True
. If the objects are not of the same type, the result of the comparison is
False
. You can test to
see if the object
theButton
references the VBScript object
Button
as follows:
Answer = theButton Is Button
If Answer = True Then
WScript.Echo “theButton is equivalent to Button.”
Else
WScript.Echo “theButton is NOT equivalent to Button.”
End If
You can also perform the comparison directly in an
if
statement:
If theButton Is Button Then
WScript.Echo “theButton is equivalent to Button.”
Else
WScript.Echo “theButton is NOT equivalent to Button.”
End If
86804c02.indd 25
86804c02.indd 25
1/21/09 1:24:30 PM
1/21/09 1:24:30 PM
26
Part
I
Getting Started with Windows Scripting
Performing operations on strings
The most common string operations you’ll want to perform are assignment and concatenation. You
assign values to strings using the equals sign, ensuring that the value is enclosed in double quota-
tion marks, such as:
aString = “This is a String.”
Concatenation is the technical term for adding strings together. Although you can use the
+
operator
to concatenate strings, the normal operator for string concatenation is the
&
operator. Using the
&
operator, you can add strings together as follows:
custAddress = streetAdd & “ “ & cityState & “ “ & zipCode
Sometimes you may also want to display the value of a string in a message box. In such an instance,
you will use the
&
operator as well. For example:
aString = “I get it!”
WScript.Echo “The string value is: “ & aString
would display a dialog box with the message:
The string value is: I get it!
Conditional Statements
Traffic lights control the flow of traffic on the street. Conditional instructions control the flow of
instructions in your code.
Using If… Then
You use
If
statements to execute a set of instructions only when certain conditions are met. In
VBScript,
If…Then
structures follow this syntax:
If condition = True Then
‘Handle the condition
End If
or
If condition Then
‘Handle the condition
End If
86804c02.indd 26
86804c02.indd 26
1/21/09 1:24:30 PM
1/21/09 1:24:30 PM
27
VBScript
Essentials
2
Note the use of the
End If
statement. This is what makes it possible to execute multiple commands
when a condition exists, such as:
If sum > 25 Then
WScript.Echo “The sum exceeds the expected Result”
‘Reset sum to zero
sum = 0
End If
You can control the execution of instructions based on a false condition as follows:
If condition = False Then
‘The condition is false
End If
or
If Not condition Then
‘The condition is false
End If
Using Else and ElseIf
You can extend the
If
…
Then
condition with
Else
statements. The
Else
statement provides an alter-
native when a condition that you specified is not met. The structure of an
If
…
Then Else
statement
is as follows:
If checkValue = “Yes” Then
WScript.Echo “The condition has been met.”
Else
WScript.Echo “The condition has not been met.”
End If
To add more conditions, you can use
ElseIf
statements. Each additional condition you add to the
code is then checked for validity. An example using
ElseIf
is shown in Listing 2-4.
LISTING 2-4
Working with ElseIf
testvalue.vbs
currValue = 5
If currValue < 0 Then
WScript.Echo “The value is less than zero.”
ElseIf currValue = 0 Then
WScript.Echo “The value is equal to zero.”
continued
86804c02.indd 27
86804c02.indd 27
1/21/09 1:24:30 PM
1/21/09 1:24:30 PM
Dostları ilə paylaş: |