Home

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.

Same As

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:

Not Equal

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.

 

Previous Copyright © 2006 FunctionX, Inc. Next