Computer programs make extensive use of comparative operations to determine which of two or more processing routines to execute depending on a comparison of data values. If, for instance, one variable is larger in value than a second variable, then a particular group of processing statements is executed. On the other hand, if the first variable is smaller than the second variable, then a different groups of processing statements is executed. This ability of programs to take alternative courses of action depending on comparative data values is made possible with relational operators and logical operators.
Relational Operators
Visual Basic supplies a set of six relational operators to compare the magnitudes of two data values. Comparisons can be made with numeric or string values, with a Boolean value returned from the comparison. The general format for the comparison statement is
| value operator value |
The following table summarizes these relational operators.
| Operator | Use |
|---|---|
| = | Tests whether two values are equal. |
| <> | Tests whether two values are not equal. |
| < | Tests whether the first value is less than the second value. |
| > | Tests whether the first value is greater than the second value. |
| <= | Tests whether the first value is less than or equal to the second value. |
| >= | Tests whether the first value is greater than or equal to the second value. |
In the following example, the Boolean value False is assigned to variable Result1 and the Boolean value True is assigned to Result2. Value1 is not equal to Value2; rather, Value1 is less than Value2.
Dim Value1 As Decimal = 10 Dim Value2 As Decimal = 20 Dim Result1 As Boolean Dim Result2 As Boolean Result1 = Value1 = Value2 Result2 = Value1 < Value2
Comparisons should be made between similar data types. If this is not the case, say in comparing a number with a string, Visual Basic attempts to make sense of the test and perform any type conversions necessary to carry out the comparison.
Logical Operators
Relational tests can be combined to test multiple conditions at the same time. The logical operators AND, OR, NOT, and Xor are used as conjunctions between multiple relational tests.
In the following example, all of the complex relational tests are True. Notice in the third relational test that parentheses are used, just as in composing arithmetic expressions, to control the order in which tests are made.
Dim Value1 As Decimal = 10 Dim Value2 As Decimal = 20 Dim Value3 As Decimal = -5 Dim Result1 As Boolean Dim Result2 As Boolean Dim Result3 As Boolean Result1 = Value1 < Value2 AND Value1 > Value3 Result2 = Value1 > Value2 OR Value1 > Value3 Result3 = Value1 < Value2 AND (Value1 > Value3 OR Value2 < Value3)
The Xor (exclusive OR) operation tests whether one of two comparisons, but not the other, is True. Consider the following expressions based on the values shown above (parentheses are used to make the relational tests visually obvious).
Result4 = (Value1 < Value2) OR (Value1 > Value3) Result5 = (Value1 < Value2) Xor (Value1 > Value3)
Both relational tests are True (Value1 < Value2 and Value1 > Value3). In the first statement, Result4 is True since either of the relational tests is True; it doesn't matter that both are True. In the second statement, Result5 is False since one or the other test, exclusively, must be True, but not both.
Comparison Functions
It is often necessary to test the contents of variables and other data containers to determine if they contain values and, if so, whether they contain a particular data type. For example, the user may be expected to enter data into a textbox for processing by a script, with processing contingent on having a proper data type available.
Testing for the presence of data can be performed with a relational operation such as TextBox.Text = "". This test returns True if there is no value in the textbox; the textbox is empty or contains a null value. It returns False if data is present. (Usually, this test is formulated as TextBox.Text <> "", testing for the expected condition that data is present.)
In testing for expected data types, Visual Basic supplies a couple of functions. IsNumeric() tests for a numeric value; IsDate() tests for a date/time data type. Thus, the test IsNumeric(TextBox.Text) returns True if the textbox contains a number; it returns False if the textbox is empty or if it does not contain a number. The same return values are produced when testing for a date/time type.
Program Decision Making
Relational and logical tests are normally set up as part of a program decision-making structure, using the Visual Basic If statement to make the test, for instance,
If Variable_A = Variable_B Then ...process these statements Else ...process these statements End If
If IsNumeric(TextBox.Text) Then ...process these statements Else ...process these statements End If
Program decision making is covered later in these tutorials at which time additional examples of relational and logical operations are presented.