Conditional Statements: 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 System; public enum HouseType { Unknown, SingleFamily, Townhouse, Condominium }```
```class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
decimal value = 0M;

Console.WriteLine("Enter the type of house you want to purchase");
Console.WriteLine("1. Single Family");
Console.WriteLine("2. Townhouse");
Console.WriteLine("3. Condominium");
Console.Write("You Choice? ");

if (choice == 1)
type = HouseType.SingleFamily;
if (choice == 2)
type = HouseType.Townhouse;
if (choice == 3)
type = HouseType.Condominium;

Console.Write("Up to how much can you afford? \$");

Console.WriteLine("\nDesired House Type:      {0}", type);
Console.WriteLine("Maximum value afforded:  {0:C}\n", value);
}
}```

Suppose a customer responds to these questions: she indicates that she wants single family house but she cannot afford more than \$550,000:

```Enter the type of house you want to purchase
1. Single Family
2. Townhouse
3. Condominium
You Choice? 1
Up to how much can you afford? \$550000

Desired House Type:      SingleFamily
Maximum value afforded:  \$550,000.00

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 \$550,001. We can create two statements as follows:

1. The house is single family
2. The house costs less than \$550,001

From our list of real estate 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 \$550,000, we retain it:

 Price Range Value \$550,001 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 \$550,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 \$625,000 Town House AND \$625,000 False False False

In C#, the Boolean AND operator is performed using the && operator. Here is an example:

```using System;

public enum HouseType
{
Unknown,
SingleFamily,
Townhouse,
Condominium
}

class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
decimal value = 0M;

Console.WriteLine("Enter the type of house you want to purchase");
Console.WriteLine("1. Single Family");
Console.WriteLine("2. Townhouse");
Console.WriteLine("3. Condominium");
Console.Write("You Choice? ");

Console.Write("Up to how much can you afford? \$");

if(choice == 1)
type = HouseType.SingleFamily;
if (choice == 2)
type = HouseType.Townhouse;
if (choice == 3)
type = HouseType.Condominium;
Console.WriteLine("\nDesired House Type:      {0}", type);
Console.WriteLine("Maximum value afforded:  {0:C}", value);

if (type == HouseType.SingleFamily && value <= 550000)
Console.WriteLine("\nDesired House Matched");
}
}```

Here is an example of running the program:

```Enter the type of house you want to purchase
1. Single Family
2. Townhouse
3. Condominium
You Choice? 1
Up to how much can you afford? \$450000

Desired House Type:      SingleFamily
Maximum value afforded:  \$450,000.00

Desired House Matched
Press any key to continue . . .```

By definition, a logical conjunction combines two conditions. To make the program easier to read, each side of the conditions can be included in parentheses. Here is an example:

```using System;

public enum HouseType
{
Unknown,
SingleFamily,
Townhouse,
Condominium
}

class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
decimal value = 0M;

. . .

if( (type == HouseType.SingleFamily) && (value <= 550000) )
Console.WriteLine("\nDesired House Matched");
}
}```

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 we consider the second criterion. Suppose that the house we are considering costs \$750,500: the price is out of the customer's 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 \$750,500 Single Family AND \$750,500 True False False

This can be illustrated by the following run of the program:

```using System;

public enum HouseType
{
Unknown,
SingleFamily,
Townhouse,
Condominium
}

class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
decimal value = 0M;

Console.WriteLine("Enter the type of house you want to purchase");
Console.WriteLine("1. Single Family");
Console.WriteLine("2. Townhouse");
Console.WriteLine("3. Condominium");
Console.Write("You Choice? ");

if (choice == 1)
type = HouseType.SingleFamily;
if (choice == 2)
type = HouseType.Townhouse;
if (choice == 3)
type = HouseType.Condominium;

Console.Write("Up to how much can you afford? \$");

Console.WriteLine("\nDesired House Type:      {0}", type);
Console.WriteLine("Maximum value afforded:  {0:C}", value);

if (type == HouseType.SingleFamily && value <= 550000)
Console.WriteLine("\nDesired House Matched");
else
Console.WriteLine("\nThe House Doesn't Match the Desired Criteria");
}
}```

Here is an example of running the program:

```Enter the type of house you want to purchase
1. Single Family
2. Townhouse
3. Condominium
You Choice? 1
Up to how much can you afford? \$750000

Desired House Type:      SingleFamily
Maximum value afforded:  \$750,000.00

The House Doesn't Match the Desired Criteria
Press any key to continue . . .```

Suppose we find a townhouse that costs \$420,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 \$420,000 Town House AND \$420,000 False True False

Here is an example of running the above program:

```Enter the type of house you want to purchase
1. Single Family
2. Townhouse
3. Condominium
You Choice? 2
Up to how much can you afford? \$420000

Desired House Type:      Townhouse
Maximum value afforded:  \$420,000.00

The House Doesn't Match the Desired Criteria
Press any key to continue . . .```

If we find a single family home that costs \$345,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 \$345,000 Single Family AND \$345,000 True True True

This can be revealed in the following run of the above program:

```Enter the type of house you want to purchase
1. Single Family
2. Townhouse
3. Condominium
You Choice? 1
Up to how much can you afford? \$345000

Desired House Type:      SingleFamily
Maximum value afforded:  \$345,000.00

Desired House Matched
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 a single family home
2. The house costs less than \$450,001
3. The house has an indoor garage

Here is the program that could be used to check these conditions:

```using System;

public enum HouseType
{
Unknown,
SingleFamily,
Townhouse,
Condominium
}

class Program
{
static void Main()
{
HouseType type = HouseType.Unknown;
int choice;
decimal value = 0M;
bool hasIndoorGarage;

Console.WriteLine("Enter the type of house you want to purchase");
Console.WriteLine("1. Single Family");
Console.WriteLine("2. Townhouse");
Console.WriteLine("3. Condominium");
Console.Write("You Choice? ");

if (choice == 1)
type = HouseType.SingleFamily;
if (choice == 2)
type = HouseType.Townhouse;
if (choice == 3)
type = HouseType.Condominium;

Console.Write("Up to how much can you afford? \$");

Console.Write("Does the house have an indoor garage (1=Yes/0=No)? ");

Console.WriteLine("\nDesired House Type:      {0}", type);
Console.WriteLine("Maximum value afforded:  {0:C}", value);
Console.Write("House has indoor garage: ");
if (ans == 1)
Console.WriteLine("Yes");
else
Console.WriteLine("No");

if ((type == HouseType.SingleFamily) && (value <= 550000) && (ans == 1))
Console.WriteLine("\nDesired House Matched");
else
Console.WriteLine("\nThe House Doesn't Match the Desired Criteria");
}
}```

We 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:

 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 True False False

To verify this, here is an example of running the program:

```Enter the type of house you want to purchase
1. Single Family
2. Townhouse
3. Condominium
You Choice? 1
Up to how much can you afford? \$425000
Does the house have an indoor garage (1=Yes/0=No)? 0

Desired House Type:      SingleFamily
Maximum value afforded:  \$425,000.00
House has indoor garage: No

The House Doesn't Match the Desired Criteria
Press any key to continue . . .```

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