Logical Conjunction: AND |
|
Imagine that a real estate agent who will be using your program is meeting with a potential buyer and asking questions from the following program: using namespace System; int main() { __wchar_t TypeOfHome = L'U'; // Default: Unknown double Value = 0; // Default: Any value Console::WriteLine(L"Enter the type of house you want to purchase"); Console::WriteLine(L"S - Single Family"); Console::WriteLine(L"T - Town House"); Console::WriteLine(L"C - Condominium"); Console::Write(L"Your Choice: "); TypeOfHome = __wchar_t::Parse(Console::ReadLine()); Console::Write(L"Up to how much can you afford? $"); Value = double::Parse(Console::ReadLine()); Console::WriteLine(); Console::WriteLine(L"Type of Home: {0}", TypeOfHome); Console::WriteLine(L"Maximum value desired: ${0}", Value); return 0; } Suppose a customer responds to these questions: she indicates that she wants single family house but she cannot afford more than $350,000: Enter the type of house you want to purchase S - Single Family T - Town House C - Condominium Your Choice: S Up to how much can you afford? $350000 Type of Home: S Maximum value desired: $350000 Press any key to continue . . . When considering a house for this customer, there are two details to be validated here: the house must be a single family home, second, it must cost less than $350,001. We can create two statements as follows:
From our list of properties, if we find a house that is a single family home, we put it in our list of considered properties:
On the other hand, if we find a house that is less than or equal to $350,000, we retain it:
One of the ways you can combine two comparisons is by joining them. For our customer, we want a house to meet BOTH criteria. If the house is a town house, based on the request of our customer, its conditional value is false. If the house is more than $350,000, the value of the Boolean Value is true. The Boolean operator used to join two criteria is called AND. This can be illustrated as follows:
In C++, the Boolean AND operator is performed using the && operator. Here is an example: using namespace System; int main() { enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }; HouseType TypeOfHome = TownHouse; double Value = 0; bool HouseIsSingleFamily; bool HouseIsLessThan350000; bool HouseIsSingleFamilyANDIsLessThan350000; Value = 550000; HouseIsSingleFamily = (TypeOfHome == 'T'); HouseIsLessThan350000 = (Value <= 350000); HouseIsSingleFamilyANDIsLessThan350000 = (HouseIsSingleFamily && HouseIsLessThan350000); Console::WriteLine(L"House is Single Family: {0}", HouseIsSingleFamily); Console::WriteLine(L"House is Less Than 350,000: {0}", HouseIsLessThan350000); Console::WriteLine(L"House is as Desired: {0}", HouseIsSingleFamilyANDIsLessThan350000); Console::WriteLine(); return 0; } This would produce: House is Single Family: False House is Less Than 350,000: False House is as Desired: False Press any key to continue . . . Suppose we find a single family home. The first condition is true for our customer. With the AND Boolean operator, if the first condition is true, then only do we consider the second criteria. Supposed that the house we are considering costs $550,500. This means that the price is out of her range. Therefore, the second condition is false. In the AND Boolean algebra, if the second condition is false, even if the first is true, the whole condition is false. This would produce the following table:
This can be illustrated by the following program: using namespace System; int main() { enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }; HouseType TypeOfHome = SingleFamily; double Value = 0; bool HouseIsSingleFamily; bool HouseIsLessThan350000; bool HouseIsSingleFamilyANDIsLessThan350000; Value = 550500; HouseIsSingleFamily = (TypeOfHome == SingleFamily); HouseIsLessThan350000 = (Value <= 350000); HouseIsSingleFamilyANDIsLessThan350000 = (HouseIsSingleFamily && HouseIsLessThan350000); Console::WriteLine(L"House is Single Family: {0}", HouseIsSingleFamily); Console::WriteLine(L"House is Less Than 350,000: {0}", HouseIsLessThan350000); Console::WriteLine(L"House is as Desired: {0}", HouseIsSingleFamilyANDIsLessThan350000); Console::WriteLine(); return 0; } This would produce: House is Single Family: True House is Less Than 350,000: False House is as Desired: False Press any key to continue . . . Suppose we find a townhouse that costs $320,000. Although the second condition is true, the first is false. In Boolean algebra, an AND operation is false if either condition is false:
This can be shown in the following program: using namespace System; int main() { enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }; HouseType TypeOfHome = TownHouse; double Value = 0; bool HouseIsSingleFamily; bool HouseIsLessThan350000; bool HouseIsSingleFamilyANDIsLessThan350000; Value = 320000; HouseIsSingleFamily = (TypeOfHome == SingleFamily); HouseIsLessThan350000 = (Value <= 350000); HouseIsSingleFamilyANDIsLessThan350000 = (HouseIsSingleFamily && HouseIsLessThan350000); Console::WriteLine(L"House is Single Family: {0}", HouseIsSingleFamily); Console::WriteLine(L"House is Less Than 350,000: {0}", HouseIsLessThan350000); Console::WriteLine(L"House is as Desired: {0}", HouseIsSingleFamilyANDIsLessThan350000); Console::WriteLine(); return 0; } This would produce: House is Single Family: False House is Less Than 350,000: True House is as Desired: False Press any key to continue . . . If we find a single family home that costs $315,000, both conditions are true. In Boolean algebra, an AND operation is true if BOTH conditions are true. This can be illustrated as follows:
This can be revealed in the following program: using namespace System; int main() { enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }; HouseType TypeOfHome = SingleFamily; double Value = 0; bool HouseIsSingleFamily; bool HouseIsLessThan350000; bool HouseIsSingleFamilyANDIsLessThan350000; Value = 315000; HouseIsSingleFamily = (TypeOfHome == SingleFamily); HouseIsLessThan350000 = (Value <= 350000); HouseIsSingleFamilyANDIsLessThan350000 = (HouseIsSingleFamily && HouseIsLessThan350000); Console::WriteLine(L"House is Single Family: {0}", HouseIsSingleFamily); Console::WriteLine(L"House is Less Than 350,000: {0}", HouseIsLessThan350000); Console::WriteLine(L"House is as Desired: {0}", HouseIsSingleFamilyANDIsLessThan350000); Console::WriteLine(); return 0; } This would produce: House is Single Family: True House is Less Than 350,000: True House is as Desired: True Press any key to continue . . . These four tables can be resumed as follows:
As you can see, a logical conjunction is true only of BOTH conditions are true. As seen above, the logical conjunction operator is used to combine two conditions. In some cases, you will need to combine more than two conditions. Imagine a customer wants to purchase a single family house that costs up to $450,000 with an indoor garage. This means that the house must fulfill these three requirements:
We also saw that when two conditions are combined, the compiler first checks the first condition, followed by the second. In the same way, if three conditions need to be considered, the compiler evaluates the truthfulness of the first condition:
If the first condition (or any condition) is false, the whole condition is false, regardless of the outcome of the other(s). If the first condition is true, then the second condition is evaluated for its truthfulness:
If the second condition is false, the whole combination is considered false:
When evaluating three conditions, if either the first or the second is false, since the whole condition would become false, there is no reason to evaluate the third. If both the first and the second conditions are false, there is also no reason to evaluate the third condition. Only if the first two conditions are true will the third condition be evaluated whether it is true:
The combination of these conditions in a logical conjunction can be written as A && B && C. If the third condition is false, the whole combination is considered false:
From our discussion so far, the truth table of the combinations can be illustrated as follows:
The whole combination is true only if all three conditions are true. This can be illustrated as follows:
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|