Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Comparison Operators

All comparison operators produce true or false results. In other words, the comparison is either true or the comparison is false. The mathematical operators produce numeric values, whereas the comparison operators produce only true or false values. The rest of the program can use the true or false comparison operator result to make decisions. If a comparison operator returns False when comparing whether an employee worked the last pay period, the rest of the program knows not to print a paycheck for that employee.

Comparison operators are operators that compare data values against each other and produce true or false results.

Table 7.1 describes VB's six comparison operators. The comparison operators always compare data, and that comparison is either true or false because data either compares as expected or does not.

Table 7.1. The comparison operators determine how data compares.

Operator Use Description
>

lblSales.

Caption > Goal

The greater than operator returns True if the value on the left side of > is numerically or alphabetically greater than the value on the right.
< Pay < 2000.00 The less than operator returns True if the value on the left side of < is numerically or alphabetically less than the value on the right.
= Age = Limit The equal to operator (or equal operator) returns True if the values on both sides of = are equal to each other.
>= FirstName >= "Mike" The greater than or equal to operator returns True if the value on the left side of >= is numerically or alphabetically greater than or equal to the value on the right.
<= Num <= lblAmt.Caption The less than or equal to operator returns True if the value on the left side of <= is numerically or alphabetically less than or equal to the value on the right.
<> txtAns.Text <> "Yes" The not equal to operator returns True if the value on the left side of <> is numerically or alphabetically unequal to the value on the right.

As you can see from Table 7.1, the comparison operators compare either variables, literals, control values, or combinations of all those data sources. The comparison operators work on both numeric and alphabetic values. You can compare any kind of number against another number, or any kind of string against another string.

The Comparison's Nature

When you compare strings, Visual Basic uses the ASCII table to determine how to compare the characters. For example, the ASCII table says that the uppercase letter A—with an ASCII numeric value of 65—is less than the uppercase letter B, which has an ASCII numeric value of 66. Notice that all uppercase letters are less than lowercase letters. Therefore, the abbreviation ST is less than St.

An ASCII table contains a list of characters with corresponding unique numeric representations.

To understand how comparison operators work, you must understand how to use their true or false results. The If statement, introduced in the next section, explains how you can use true and false results to make decisions in your program. Before you read the next section, make sure that you understand how these operators compare values. Make sure that you understand the Result column of Table 7.2 before you go any further.

Table 7.2. Relationship results.

Relation Result
4 > 2 True
4 < 1 False
4 < 8 True
"Apple" <= "Orange" True
"Macmillan" < "Mc millan" True
0 >= 0 True
0 <= 0 True
1 <> 2 True
2 >= 3 False

Keep Each Side's Datatype Consistent

Take extra care that the expressions on both sides of a comparison operator conform to the same datatype or at least compatible datatypes. In other words, you cannot compare a string to a numeric datatype. If you try, you will receive a type mismatch error because the datatypes don't match. You can compare any numeric datatype against any other numeric datatype most of the time. In other words, you can test whether a single-precision value is less than or greater than an integer value.

Share ThisShare This

Informit Network