Home

Logical Conjunction: AND

 

Introduction

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:

  1. The house is single family
  2. The house costs less than $350,000

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:

Type of House House
The house is single family True

On the other hand, if we find a house that is less than or equal to $350,000, we retain it:

Price Range Value
$350,000 True

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:

Type of House House Value Result
Town House $425,000 Town House AND $425,000
False False False

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:

Type of House House Value Result
Single Family $550,500 Single Family AND $550,500
True False False

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:

Type of House House Value Result
Town House $320,000 Town House AND $320,000
False True 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:

Type of House House Value Result
Single Family $315,000 Single Family AND $315,000
True True True

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:

If Condition1 is If Condition2 is Condition1
AND
Condition2
False False False
False True False
True False False
True True True

As you can see, a logical conjunction is true only of BOTH conditions are true.

Combining Conjunctions

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:

  1. The house is single family
  2. The house costs less than $450,001
  3. The house has an indoor garage

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:

Type of House
A
Town House
False

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:

Type of House Property Value
A B
Single Family $655,000
True False

If the second condition is false, the whole combination is considered false:

A B A && B
True False 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:

Type of House Property Value Indoor Garage
A B C
Single Family $425,650 None
True True False

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:

A B A && B C A && B && C
True True False False False

From our discussion so far, the truth table of the combinations can be illustrated as follows:

A B C A && B && C
False Don't Care Don't Care False
True False Don't Care False
True True False False

The whole combination is true only if all three conditions are true. This can be illustrated as follows:

A B C A && B && C
False False False False
False False True False
True False False False
True False True False
False True False False
False True True False
True True False False
True True True True
 
 

Previous Copyright © 2006-2016, FunctionX, Inc. Next