Home

Conditional Statements: Logical Disjunction: OR

 

Introduction

Our real estate company has single family homes, townhouses, and condominiums. All of the condos have only one level, also referred to as a story. Some of the single family homes have one story, some have two and some others have three levels. All our townhouses have three levels.

Another customer wants to buy a house. The customer says that he primarily wants a condo, but if our real estate company doesn't have a condominium, that is, if the company has only houses, whatever it is, whether a house or a condo, it must have only one level (story) (due to an illness, the customer would not climb the stairs). When considering the properties of our company, we would proceed with these statements:

  1. The property is a condominium
  2. The property has one story

If we find a condo, since all of our condos have only one level, the criterion set by the customer is true. Even if we were considering another (type of) property, it wouldn't matter. This can be resumed in the following table:

Type of House House
Condominium True

The other properties would not be considered, especially if they have more than one story:

Number of Stories Value
3 False

To check for either of two conditions, in Boolean algebra, you can use an operator called OR. We can show this operation as follows:

Condominium One Story Condominium OR 1 Story
True False True

In Boolean algebra, this type of comparison is performed using the OR operator. In C#, the OR 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;
        int stories = 1;

        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? ");
        choice = int.Parse(Console.ReadLine());

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

        Console.Write("How many stories? ");
        stories = int.Parse(Console.ReadLine());

        Console.WriteLine("\nDesired House Type: {0}", type);
        Console.WriteLine("Number of Stories:  {0}", stories);

        if ((type == HouseType.Condominium) || (stories == 1))
            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? 3
How many stories? 6

Desired House Type: Condominium
Number of Stories:  6

Desired House Matched
Press any key to continue . . .

Suppose that, among the properties our real estate company has available, there is no condominium. In this case, we would then consider the other properties:

Type of House House
Single Family False

If we have a few single family homes, we would look for one that has only one story. Once we find one, our second criterion becomes true:

Type of House One Story Condominium OR 1 Story
False True True

This can be illustrated 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
How many stories? 1

Desired House Type: SingleFamily
Number of Stories:  1

Desired House Matched
Press any key to continue . . .

If we find a condo and it is one story, both criteria are true. This can be illustrated in the following table:

Type of House One Story Condominium OR 1 Story
False True True
True True True

The following run of the program demonstrates this:

Enter the type of house you want to purchase
1. Single Family
2. Townhouse
3. Condominium
You Choice? 3
How many stories? 1

Desired House Type: Condominium
Number of Stories:  1

Desired House Matched
Press any key to continue . . .

A Boolean OR operation produces a false result only if BOTH conditions ARE FALSE:

If Condition1 is If Condition2 is Condition1 OR Condition2
False True True
True False True
True True True
False False False

Here is another example of running the program:

Enter the type of house you want to purchase
1. Single Family
2. Townhouse
3. Condominium
You Choice? 2
How many stories? 2

Desired House Type: Townhouse
Number of Stories:  2

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

Combinations of Disjunctions

As opposed to evaluating only two conditions, you may face a situation that presents three of them and must consider a combination of more than two conditions.

 

Previous Copyright © 2006-2007 FunctionX, Inc. Next