Arithmetic Operations

 

Incrementing a Number

We are used to counting numbers such as 1, 2, 3, 4, etc. When counting such numbers, we are simply adding 1 to a number in order to get the next number in the range. C++ provides a technique of transparently counting such numbers.

The simplest technique of incrementing a value consists of adding 1 to a variable. After adding 1, the value or the variable is (permanently) modified and the variable would hold the new value. This is illustrated in the following example:

using namespace System;

int main()
{
    Byte Stories = 2;

    Console::WriteLine(L"Stories: {0}", Stories);
    Stories = Stories + 1;
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

This would produce:

Stories: 2
Stories: 3

Press any key to continue . . .

C++ provides a special operator that takes care of this operation. The operator is called the increment operator and is ++. Instead of writing Stories = Stories + 1, you can write Stories++ and you would get the same result. The above program can be re-written as follows:

using namespace System;

int main()
{
    Byte Stories = 2;

    Console::WriteLine(L"Stories: {0}", Stories);
    Stories++;
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

The increment++ is called a unary operator because it operates on only one variable. It is used to modify the value of the variable by adding 1 to it.

Every time the Stories++ is executed, the compiler takes the previous value of the variable and adds 1 to it and the variable holds the incremented value:

using namespace System;

int main()
{
    Byte Stories = 2;

    Console::WriteLine(L"Stories: {0}", Stories);
    Stories++;
    Console::WriteLine(L"Stories: {0}", Stories);
    Stories++;
    Console::WriteLine(L"Stories: {0}", Stories);
    Stories++;
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

This would produce:

Stories: 2
Stories: 3
Stories: 4
Stories: 5

Press any key to continue . . .

Pre and Post-Increment

When using the ++ operator, the position of the operator with regards to the variable it is modifying can be significant. To increment the value of the variable before re-using it, you should position the operator on the left of the variable. Here is an example:

using namespace System;

int main()
{
    Byte Stories = 2;

    Console::WriteLine(L"Stories: {0}", Stories);
    Console::WriteLine(L"Stories: {0}", ++Stories);
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

This would produce:

Stories: 2
Stories: 3
Stories: 3

Press any key to continue . . .

When writing ++Stories, the value of the variable is incremented before being called. On the other hand, if you want to first use a variable, then increment it, in other words, if you want to increment the variable after calling it, position the ++ operator on the right side of the variable. Here is an example:

using namespace System;

int main()
{
    Byte Stories = 2;

    Console::WriteLine(L"Stories: {0}", Stories);
    Console::WriteLine(L"Stories: {0}", Stories++);
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

This would produce:

Stories: 2
Stories: 2
Stories: 3

Press any key to continue . . .

Decrementing: Pre and Post-Decrementing

When counting numbers backward, such as 8, 7, 6, 5, etc, we are in fact subtracting 1 from a value in order to get the lesser value. This operation is referred to as decrementing a variable. This operation works as if a variable called Stories had its value diminished by 1, as in

using namespace System;

int main()
{
    Byte Stories = 4;

    Console::WriteLine(L"Stories: {0}", Stories);
    Stories = Stories - 1;
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

As done with the increment, C++ provides a quicker way of subtracting 1 from a variable. This is done using the decrement operator, that is --. To use the decrement operator, type –- on the left or the right side of the variable. Here is an example:

using namespace System;

int main()
{
    Byte Stories = 4;

    Console::WriteLine(L"Stories: {0}", Stories);
    Stories--;
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

Both versions would produce:

Stories: 4
Stories: 3

Press any key to continue . . .

Once again, the position of the operator is important. If you want to decrement the variable before calling it, position the operator on the left side of the operator. If you plan to decrement a variable after it has been accessed, position the operator on the right side of the variable.

Techniques of Incrementing and Decrementing a Variable

It is not unusual to add, or to subtract, a constant value to, or from, a variable. All you have to do is to declare another variable that would hold the new value. Here is an example:

using namespace System;

int main()
{
    double PropertyValue = 612750;
    double NewValue;

    Console::WriteLine(L"Property Value: {0:C}", PropertyValue);
    NewValue = PropertyValue + 2420;
    Console::WriteLine(L"Property Value: {0:C}", NewValue);

    Console::WriteLine();
    return 0;
}

This would produce:

Property Value: $612,750.00
Property Value: $615,170.00

Press any key to continue . . .

The above technique requires that you use an extra variable in your application. The advantage is that each variable can hold its own value although the value of the second variable depends on whatever would happen to the original or source variable. Sometimes in your program you will not need to keep the value of the source variable. You may want to simply modify the value that a variable is holding. In this case you can perform the addition operation directly on the variable by adding the desired value to the variable. This operation modifies whatever value a variable is holding and doesn't need an additional variable. To add a value to a variable and change the value that the variable is holding, use the assignment “=” and the addition “+” operators as follows:

using namespace System;

int main()
{
    double PropertyValue = 442824;

    Console::WriteLine(L"Property Value: {0:C}", PropertyValue);
    PropertyValue = PropertyValue + 12850;
    Console::WriteLine(L"Property Value: {0:C}", PropertyValue);

    Console::WriteLine();
    return 0;
}

This would produce:

Property Value: $442,824.00
Property Value: $455,674.00

Press any key to continue . . .

C++ provides an operator that combines both + and = to allow you to increase the value held by a numeric variable. To use it, type += on the right side of the variable, followed by the value by which the variable will be incremented. Here is an example:

using namespace System;

int main()
{
    double PropertyValue = 442824;

    Console::WriteLine(L"Property Value: {0:C}", PropertyValue);
    PropertyValue += 12850;
    Console::WriteLine(L"Property Value: {0:C}", PropertyValue);

    Console::WriteLine();
    return 0;
}

This would produce the same result as the previous program.

To decrease the value of a variable, instead of the addition operation, use the subtraction and apply the same techniques. In the above program, the variable can have its value decremented by applying the -= operator.

Logical Operations

 

Introduction

A logical operation is used to compare two values. The values must be of the same types. It is certainly understandable to compare two temperatures, the number of students in one class and the number of students in another class, etc. To support comparisons, the C++ language provides 6 operators that are used in the form:

Operand1 Operator Operand2

After the comparison has been performed, it produces a result recognized as being true, or as being false.

The "Same As" Operator: ==

To compare two variables for equality, C++ uses the == operator. Its syntax is:

Value1 == Value2

The Same As operator is used to find out whether two variables (or one variable and a constant) hold the same value. From this formula, the compiler would compare the value of Value1 with that of Value2. If Value1 and Value2 hold the same value, the comparison produces a true result. If they are different, the comparison renders false.

Most of the comparisons performed in C++ will be applied to conditional statements. Most of the time, you will not need to display the result to the console but you can. Here is an example:

using namespace System;

int main()
{
	int YearBuilt = 1962;

    	Console::Write(L"Comparison of YearBuilt == 1998 produces ");
	Console::WriteLine(YearBuilt == 1998);
    
	return 0;
}

This would produce:

Comparison of YearBuilt == 1998 produces False
Press any key to continue . . .

Because a Boolean operation produces a true or a false value, the result of its comparison can also be assigned to a Boolean variable. To store the result of a comparison, you should include the comparison operation between parentheses. Here is an example:

using namespace System;

int main()
{
	int YearBuiltHouse1 = 1962;
	int YearBuiltHouse2 = 1998;
	bool BuiltTheSameYear = (YearBuiltHouse1 == YearBuiltHouse2);

	Console::WriteLine(L"House1 Year Built: {0}", YearBuiltHouse1);
	Console::WriteLine(L"House2 Year Built: {0}", YearBuiltHouse2);
    	Console::Write(L"Built the same year? ");
	Console::WriteLine(BuiltTheSameYear);
    
	return 0;
}

This would produce:

House1 Year Built: 1962
House2 Year Built: 1998
Built the same year? False
Press any key to continue . . .

The "Is Not The Same As" Operator: !=

Instead of two values being equal, you may be interested to know whether they are not equal. To perform this comparison, you can use the != operator. The formula used is:

Value1 != Value2

!= is a binary operator (like all logical operators except the logical not, which is a unary operator) that is used to compare two values. The values can come from two variables as in Variable1 != Variable2. Upon comparing the values, if both variables hold different values, the comparison produces true. Otherwise, the comparison renders false:

Here is an example:

using namespace System;

int main()
{
	int YearBuiltHouse1 = 1962;
	int YearBuiltHouse2 = 1998;
	bool BuiltInDifferentYears = (YearBuiltHouse1 != YearBuiltHouse2);

	Console::WriteLine(L"House1 Year Built: {0}", YearBuiltHouse1);
	Console::WriteLine(L"House2 Year Built: {0}", YearBuiltHouse2);
    	Console::Write(L"Built in different years? ");
	Console::WriteLine(BuiltInDifferentYears);
    
	return 0;
}

This would produce:

House1 Year Built: 1962
House2 Year Built: 1998
Built in different years? True
Press any key to continue . . .

The "Less Than" Operator: <

Instead of strict equality or strict inequality, you may want to know whether one value is lower than another. To perform this comparison, you can use the < operator. Its formula is:

Value1 < Value2

The value held by Value1 is compared to that of Value2. As it would be done with other operations, the comparison can be made between two variables, as in Variable1 < Variable2. If the value held by Value1 is lower than that of Value2, the comparison produces a true result:

Less Than

Here is an example:

using namespace System;

int main()
{
	int YearBuiltHouse1 = 1962;
	int YearBuiltHouse2 = 1998;
	bool House1BuiltBeforeHouse2 = (YearBuiltHouse1 < YearBuiltHouse2);

	Console::WriteLine(L"House1 Year Built: {0}", YearBuiltHouse1);
	Console::WriteLine(L"House2 Year Built: {0}", YearBuiltHouse2);
    	Console::Write(L"House1 built before House2? ");
	Console::WriteLine(House1BuiltBeforeHouse2);
    
	return 0;
}

This would produce:

House1 Year Built: 1962
House2 Year Built: 1998
House1 built before House2? True
Press any key to continue . . .
 

The "Less Than or Equal to" Operator <=

The Less Than operator is used to find out if one value is strictly lower than another. An alternative to being so restrictive is to find out, instead of being absolutely less than another, whether both values could be equal instead. This type of comparison can be performed using the <= operator. Its formula is:

Value1 <= Value2

The <= operator performs a comparison as any of the last two. If both Value1 and Value2 hold the same value, the result is true. If the left operand, in this case Value1, holds a value lower than the second operand, in this case Value2, the result is still true:

Less Than or Equal to

Here is an example:

using namespace System;

int main()
{
	int YearBuiltHouse1 = 1962;
	int YearBuiltHouse2 = 1998;
	bool House1EitherBuiltBeforeHouse2OrTheSameYear = (YearBuiltHouse1 <= YearBuiltHouse2);

	Console::WriteLine(L"House1 Year Built: {0}", YearBuiltHouse1);
	Console::WriteLine(L"House2 Year Built: {0}", YearBuiltHouse2);
    	Console::Write(L"House1 built before House2 or the same year? ");
	Console::WriteLine(House1EitherBuiltBeforeHouse2OrTheSameYear);
    
	return 0;
}

This would produce:

House1 Year Built: 1962
House2 Year Built: 1998
House1 built before House2 or the same year? True
Press any key to continue . . .

The "Greater Than" Operator: >

If you have two values of the same type that are different, one of them is usually higher than the other. To get the result of this type of comparison, you can use the Greater Than operator >. The formula to use it is:

Value1 > Value2

Both operands, in this case Value1 and Value2, can be variables or the left operand can be a variable while the right operand is a constant. If the value on the left of the > operator is greater than the value on the right side or a constant, the comparison produces a true result. Otherwise, the comparison renders false:

Greater Than

This is an example of using the > operator:

using namespace System;

int main()
{
	int YearBuiltHouse1 = 1962;
	int YearBuiltHouse2 = 1998;
	bool House2BuiltAfterHouse1 = (YearBuiltHouse2 > YearBuiltHouse1);

	Console::WriteLine(L"House1 Year Built: {0}", YearBuiltHouse1);
	Console::WriteLine(L"House2 Year Built: {0}", YearBuiltHouse2);
    	Console::Write(L"House2 built after House1? ");
	Console::WriteLine(House2BuiltAfterHouse1);
    
	return 0;
}

This would produce:

House1 Year Built: 1962
House2 Year Built: 1998
House2 built after House1? True
Press any key to continue . . .

The "Greater Than or Equal to" Operator >=

To find out whether one value is greater than another, you can use the >= operator. This is the "greater than or equal to" operator. Its formula is:

Value1 >= Value2

A comparison is performed on both operands: Value1 and Value2. If the value of Value1 and that of Value2 are the same, the comparison produces a true result. If the value of the left operand is greater than that of the right operand, the comparison produces true result also. If the value of the left operand is strictly less than the value of the right operand, the comparison produces a false result.

Greater Than or Equal to

 

Here is an example that performs this comparison:

using namespace System;

int main()
{
	int YearBuiltHouse1 = 1962;
	int YearBuiltHouse2 = 1998;
	bool House1AndHouse2PossiblyBuiltSameYear = (YearBuiltHouse2 >= YearBuiltHouse1);

	Console::WriteLine(L"House1 Year Built: {0}", YearBuiltHouse1);
	Console::WriteLine(L"House2 Year Built: {0}", YearBuiltHouse2);
    	Console::Write(L"House1 and House2 possibly built the same year ? ");
	Console::WriteLine(House1AndHouse2PossiblyBuiltSameYear);
    
	return 0;
}

This would produce:

House1 Year Built: 1962
House2 Year Built: 1998
House1 and House2 possibly built the same year ? True
Press any key to continue . . .
 

Logical Combinations: The Logical Not Operator !

Each of the operators we have used so far was sufficient in its own right, to perform a simple or relatively simple operation. To perform more advanced comparisons, you can combine two or more operations considered as one. This is done using logical NOT, AND, or OR operators.

The six logical operators we have reviewed so far clearly each produces a logical value based on the way the operation is presented. Another aspect that the review demonstrated was that

Operator Opposite
!= ==
< >=
<= >
> <=
>= <
== !=

Therefore, each operator has a clear opposite. In some cases, you may first establish a logical operation but then you want the opposite of its result. In other words, you may want to negate the result of a logical operation. To support this, the C language provides the Not operator that its child languages (C++, C++/CLI, C#, Java, JavaScript) inherit. This operator is represented with !. Its formula is:

!Value

This operator is placed on the left side of a logical operation. To make its operand clear, the operation to be negated should be included in parentheses. Here is an example:

using namespace System;

int main()
{
	int YearBuiltHouse1 = 1962;
	int YearBuiltHouse2 = 1998;
	bool BuiltTheSameYear = (YearBuiltHouse1 == YearBuiltHouse2);
	bool NotBuiltTheSameYear = !(YearBuiltHouse1 == YearBuiltHouse2);

	Console::WriteLine(L"House1 Year Built: {0}", YearBuiltHouse1);
	Console::WriteLine(L"House2 Year Built: {0}", YearBuiltHouse2);
    	Console::Write(L"Built the same year? ");
	Console::WriteLine(BuiltTheSameYear);
    	Console::Write(L"NOT Built the same year? ");
	Console::WriteLine(NotBuiltTheSameYear);
    
	return 0;
}

This would produce:

House1 Year Built: 1962
House2 Year Built: 1998
Built the same year? False
NOT Built the same year? True
Press any key to continue . . .

It is very important to understand how the NOT operator works: it negates its operand. This means that, with the ! operator, if the operand is supposed to produce a true result, it would produce a false result. On the other hand, with the ! operator, if the operand is supposed to produce a false result, it would produce a true result.

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
 

Logical Disjunction: OR

 

Introduction

Our real estate company has single family homes, townhouses, and condominium. 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 or some others have three levels. All townhouses have three levels.

Another customer wants to buy a home. 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). 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 namespace System;

int main()
{
	enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
	const int DesiredNumberOfStories = 1;

	int Stories = 3;
	HouseType TypeOfHome = SingleFamily;

	bool HouseIsCondominium;
	bool HouseHasOneStory;
	bool HouseIsCondoORHasOneStory;

	TypeOfHome = Condominium;
	Stories    = 2;

	HouseIsCondominium = (TypeOfHome == Condominium);
	HouseHasOneStory   = (Stories == DesiredNumberOfStories);
	HouseIsCondoORHasOneStory = 
		(HouseIsCondominium || HouseHasOneStory);

	Console::WriteLine(L"House is a Condo:  {0}",
				HouseIsCondominium);
	Console::WriteLine(L"House has 1 Story: {0}",
				HouseHasOneStory);
	Console::WriteLine(L"House Either is a Condo or has One Story: {0}",
				HouseIsCondoORHasOneStory);
	Console::WriteLine();

	return 0;
}

This would produce:

House is a Condo:  True
House has 1 Story: False
House Either is a Condo or has One Story: True

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

using namespace System;

int main()
{
	enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
	const int DesiredNumberOfStories = 1;

	int Stories = 3;
	HouseType TypeOfHome = SingleFamily;

	bool HouseIsCondominium;
	bool HouseHasOneStory;
	bool HouseIsCondoORHasOneStory;

	TypeOfHome = Condominium;
	Stories    = 1;

	HouseIsCondominium = (TypeOfHome == SingleFamily);
	HouseHasOneStory   = (Stories == DesiredNumberOfStories);
	HouseIsCondoORHasOneStory = 
		(HouseIsCondominium || HouseHasOneStory);

	Console::WriteLine(L"House is a Condo:  {0}",
				HouseIsCondominium);
	Console::WriteLine(L"House has 1 Story: {0}",
				HouseHasOneStory);
	Console::WriteLine(L"House Either is a Condo or has One Story: {0}",
				HouseIsCondoORHasOneStory);
	Console::WriteLine();

	return 0;
}

This would produce:

House is a Condo:  False
House has 1 Story: True
House Either is a Condo or has One Story: True

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 program demonstrates this:

using namespace System;

int main()
{
	enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
	const int DesiredNumberOfStories = 1;

	int Stories = 3;
	HouseType TypeOfHome = SingleFamily;

	bool HouseIsCondominium;
	bool HouseHasOneStory;
	bool HouseIsCondoORHasOneStory;

	TypeOfHome = Condominium;
	Stories    = 1;

	HouseIsCondominium = (TypeOfHome == Condominium);
	HouseHasOneStory   = (Stories == DesiredNumberOfStories);
	HouseIsCondoORHasOneStory = 
		(HouseIsCondominium || HouseHasOneStory);

	Console::WriteLine(L"House is a Condo:  {0}",
				HouseIsCondominium);
	Console::WriteLine(L"House has 1 Story: {0}",
				HouseHasOneStory);
	Console::WriteLine(L"House Either is a Condo or has One Story: {0}",
				HouseIsCondoORHasOneStory);
	Console::WriteLine();

	return 0;
}

This would produce:

House is a Condo:  True
House has 1 Story: True
House Either is a Condo or has One Story: True

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
 

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-2025, FunctionX Monday 14 October 2024, 10:12 Next