
http://www.ronshardwebapps.com
September, 2003
VBScript Operators: Part II
Thanks for your patience. I will try to keep these on schedule in the future!
|
The Topic:
VBScript Operators Part I discussed the mathematical and
assignment operators.
In this tutorial we will cover comparison and concatenation operators.
Next month we will discuss VBScript decision structures (IF...Then and Select Case statements), along
with logical operators (AND, OR) and tie it all together with the operators we discussed in Parts I and II of the VBScript Operators tutorials.
|
Who is this lesson for:
This VBScript tutorial is for beginners who are just becoming familiar with ASP and VBScript, or for
experienced programmers who need a reference on operators.
|
The Comparison Operators:
In VBScript there are 5 basic ways to compare string or numeric values. When you
use one of the comparison operators, VBScript will compare the two values you submit and return true or false depending on the comparison you
are making. The following table lists the comparison operators for VBScript (examples of each
will be given below):
Operator
|
Description
|
Test
|
=
|
equal to
|
returns TRUE if values are equal
|
>
|
greater than
|
returns TRUE if first value is greater than the second
|
<
|
less than
|
returns TRUE if first value is less than the second
|
>=
|
greater than or equal to
|
returns TRUE if first value is greater than or equal to the second
|
<=
|
less than or equal to
|
returns TRUE if first value is less than or equal to the second
|
|
Here are a few examples of each of the comparison operators in use:
Equal To Operator (=)
Test
|
Result
|
10 = (2 * 5) *
|
returns TRUE
|
"tom" = "Tom"
|
returns False
|
"Tom" = "Tom"
|
returns TRUE
|
|
Comparisons can be made between two variables or a variable and literal string or numeric value...
|
Variable Assignment
|
Test
|
Result
|
iNumber1 = 5
iNumber2 = 2 + 3
|
iNumber1 = iNumber2
|
returns True
|
strName = "Ron Williams"
|
strName = "Tom Jones"
|
returns False
|
|
*See VBScript Operators Part I
for a discussion of the VBScript mathematical operators.
|
|
Greater Than Operator (>)
Test
|
Result
|
10 > (2 * 5)
|
returns False
|
"tom" > "Tom"
|
returns True**
|
"Tom" > "Tom"
|
returns False
|
|
With variable assignments...
|
Variable Assignment
|
Test
|
Result
|
iNumber1 = 5
iNumber2 = 2 + 1
|
iNumber1 > iNumber2
|
returns True
|
strName = "Ron Williams"
|
strName > "Tom Jones"
|
returns False
|
|
**
't' is ASCII code 116, and 'T' is ASCII code 84, therefore 't' is greater than 'T' and 'tom' is greater than 'Tom'.
See the ASCII code chart for additional information.
|
|
Less Than Operator (<)
Test
|
Result
|
10 < (2 * 5)
|
returns False
|
"tom" < "Tom"
|
returns False
|
"Tom" < "Tom"
|
returns False
|
|
With variable assignments...
|
Variable Assignment
|
Test
|
Result
|
iNumber1 = 5
iNumber2 = 2 + 1
|
iNumber1 < iNumber2
|
returns False
|
strName = "Ron Williams"
|
strName < "Tom Jones"
|
returns True
|
|
Greater Than or Equal To Operator (>=)
Test
|
Result
|
10 >= (2 * 5)
|
returns True
|
"tom" >= "Tom"
|
returns True
|
"Tom" >= "Tom"
|
returns True
|
|
With variable assignments...
|
Variable Assignment
|
Test
|
Result
|
iNumber1 = 5
iNumber2 = 2 + 1
|
iNumber1 >= iNumber2
|
returns True
|
strName = "Ron Williams"
|
strName >= "Tom Jones"
|
returns False
|
|
Less Than or Equal To Operator (<=)
Test
|
Result
|
10 <= (2 * 5)
|
returns True
|
"tom" <= "Tom"
|
returns False
|
"Tom" <= "Tom"
|
returns True
|
|
With variable assignments...
|
Variable Assignment
|
Test
|
Result
|
iNumber1 = 5
iNumber2 = 2 + 1
|
iNumber1 <= iNumber2
|
returns False
|
strName = "Ron Williams"
|
strName <= "Tom Jones"
|
returns True
|
|
The Concatenation Operator:
The VBScript concatenation operator (&) can be used to combine two strings together. The
addition operator (+) will act as a concatenation operator if it is used in conjunction
with string values. VBScript treats all variables as string variants, unless you explicitly
tell it what data-type your variable contains. Actually, VBScript will take a guess on the
data-type, but to be safe, you should always use the data-type conversion functions to ensure you get the
results you want (we will discuss this in the tutorial on VBScript datatypes in November. For
more information, see the VBScript Language Reference at
http://msdn.microsoft.com). It is important to note that any characters or numbers contained
between double quotes (") will be interpreted as a string value by VBScript.
Here are some examples of concatenation:
Code
|
Result
|
Response.Write "Ron" & "Williams"
|
RonWilliams
|
Response.Write "Ron" & " " & "Williams"
|
Ron Williams
|
Response.Write 1 & 2
|
12
|
Response.Write 1 + 2
|
3
|
Response.Write "1" + "2"
|
12
|
Response.Write "Ron" + " Williams"
|
Ron Williams
|
If you have any questions about September's tutorial, send me an email
and I'll be happy to help. See you in October!
-Ron
Donations & Tips

 
 
[ Home | Top |
Guest Book ]
|
Problems, Questions, FeedBack, Suggestions?
Please Email Ron
© 2002 Ron Williams
64.71.40.22
|