Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 5: Decision
Last change: February 13 2016 18:47:58.

Logical Operators

Logical operators are primarily used in logical expressions to perform operations on Boolean data. For example, the following code tests whether the conditions that the purchase amount (named Purchase) is greater than $10,000 and that the account has no previous balance (named Balance) are both true:

Status = Purchase > 10000 And Balance = 0

Notice that there is the And logical operator between the two comparisons. The And operator requires both of the comparisons to be True to produce a result of True; that is, the variable, Status, will have a value True if the purchase amount is greater than $10,000 and the previous balance is equal to zero.
The following table provides a list of the logical operators often used in programs.

Logical Operator Example Remark/Explanation
Not Not (A = B) The Not operator negates the operand on its right; therefore, in the example if A is equal to B, the entire expression will be False.
And Purchase > 10000 And Balance = 0 When the expressions on both sides of the And operator are True, the result is True; otherwise, False.
AndAlso Purchase > 10000 AndAlso Balance = 0 The result is the same as the And operator, but the operator works differently. When the first expression is false (Purchase is not greater than 10000), the entire expression is false. The second portion is not evaluated.
Or Purchase > 10000 Or Balance = 0 When either expression on both sides of the Or operator is True, the result is True; otherwise, False.
OrElse Purchase > 10000 OrElse Balance = 0 The result is the same as the Or operator, but the operator works differently. When the first expression is True (Purchase is greater than 10000), the entire expression is true. The second portion is not evaluated.
Xor Purchase >10000 Xor Balance = 0 When exactly one of the two expressions on both sides of the Xor (exclusive Or) operator is True, the result is True; otherwise False. That is, when both sides are True or when both are False, the result is False.

Notice the difference between And and AndAlso and that between Or and OrElse. In general, And and AndAlso should produce the same result; so do Or and OrElse. The differences lie in their ways of obtaining the results and the implication thereof. Both the AndAlso and OrElse operators employ a quick out algorithm. In the case of AndAlso, the result is true only if both operands (expressions) are true, similar to the And operator; however, in the process of evaluation, when the first expression is false, it knows the result must be false and therefore will not evaluate the second expression. By the same token, in the case of OrElse, the result is true if the first expression is true. The operator will not evaluate the second expression. On the other hand, both the And and Or operators evaluate both of their operands regardless of the outcome of the first expression; therefore, AndAlso and OrElse are more efficient. This point is discussed further in Section 5. You may then wonder why there is a need for the And and Or operators. The answer is given at the end of this sub-section.

Uses of Logical Operators

Here are two examples of using the logical operators:
Example 1. A clinic patient who is insured and assigns the right to claim the insurance will be charged a co-payment, while his insurance company will pay for the remaining charges. Assume that the variables, Insured and Assigned, have a value “Y” if the patient is insured, and has assigned the insurance claim to the clinic. To compute the charge to each party, you can code the following:

If Insured = “Y” And Assigned = “Y” Then ChargeToPatient = CoPay ChargeToInsurance = TotalCharge - ChargeToPatient End If

Example 2. A store will give a 20% discount to a customer who is either a student or has purchased at least $1,000 worth of goods. Assume that the chkStudent check box is used to identify whether a customer is a student, and the Purchase variable represents the amount of purchase. The computation of discount can be coded as follows:

If chkStudent.Checked Or Purchase >= 1000 Then Discount = Purchase * .2 End If

The Xor Operator

Although the first three logical operators correspond well to the daily life connotation, the Xor operator may call for some additional explanation. As explained in the table, this operator sets the result to True only if exactly one of its two operands is True. An easy way to understand how it works is to think of it as an inequality operator; that is, when its two Boolean operands are not equal, the result is true. As its name implies, this operator is most useful in handling a situation where the conditions are mutually exclusive. For example, when making a journal entry in accounting records, the user can enter only a debit or credit amount for an account involved in that transaction. Assume the two text boxes, txtDebit and txtCredit, are used for the user to enter the amount and only positive amounts can be entered. The following code can be used to check that one and only one amount is entered:

If Val(txtDebit.Text) > 0 Xor Val(txtCredit.Text) > 0 Then ‘Only one amount has been entered; OK Else ‘Either both are zero or both amounts are entered. ‘Display a message MsgBox “Please enter exactly one amount for the account.” End If

Bit-Wise Operations

The logical operators, except for AndAlso and OrElse, actually operate on a bit-wise basis. When the operands are numeric data of Short, UShort, Integer, UInteger, Long ,or ULong type rather than the Boolean type, the results can be different from what are expected. Appendix A, “Number Systems and Bit-wise Operations,” discusses the additional fine points and some of the uses of these operators in this aspect.
Last change: February 13 2016 18:47:57.