Home

Logical Operators

 

Introduction

A program is a series of instructions that ask the computer (actually the compiler) to check some situations and to act accordingly. To check such situations, the computer spends a great deal of its time performing comparisons between values. A comparison is a Boolean operation that produces a true or a false result, depending on the values on which the comparison is performed.

A comparison is performed between two values of the same type; for example, you can compare two numbers, two characters, or the names of two cities. On the other hand, a comparison between two disparate values doesn't bear any meaning. For example, it is difficult to compare a telephone number and somebody's age, or a music category and the distance between two points. Like the binary arithmetic operations, the comparison operations are performed on two values. Unlike arithmetic operations where results are varied, a comparison produces only one of two results. The result can be a logical true or false. When a comparison is true, it has an integral value of 1 or positive; that is, a value greater than 0. If the comparison is not true, it is considered false and carries an integral value of 0.

The C# language is equipped with various operators used to perform any type of comparison between similar values. The values could be numeric, strings, or objects (operations on objects are customized in a process referred to as Operator Overloading).

There are primary assumptions you should make when writing statements used in conditions:

  • Simplicity and Clarity: A statement should be clear enough and possibly simple but as complete as possible. When a statement becomes long, it can lead to being segmented in short parts, which deceives its clarity and may create other issues.
  • Factual: The statement must be presented as fact and not as opinion. This means that you don't have to like the statement but the majority, including you, must agree that it is true. In fact, the statement doesn't have to be correct but it must be agreed upon to be true. Based on this, a statement such as "An hour contains 45 minutes" doesn't have to fit your way of thinking but it must be considered as true or as false. A statement such as "This job applicant is attractive" is an opinion and therefore must not be considered in a conditional statement. 
  • Circumstantial Truthfulness: At the time the statement is made, it must be considered as true even if it can change at another time. For example, suppose that, in a certain year, a statement is formulated as "This year, the month of February has 28 days". Although allowed, you should refrain from regularly using circumstantial truthfulness, unless you have to.
  • Inverse: A statement must be able to find its reverse. This means that, when a statement is made and decided upon to be true or false, an inverse statement must be found to make it false or true. For example, if you have a statement such as "This job applicant is 18 years old", you must be able to state that "This job applicant is not 18 years old" or "This job applicant is younger than 18".

In your programs, make sure you clearly formulate your statements. This would make your programs easy to read and troubleshoot when problems occur.

 

The Equality Operator ==

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

Value1 == Value2

The equality operation is used to find out whether two variables (or one variable and a constant) hold the same value. From our syntax, 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. The result of a comparison can also be assigned to a variable. To store the result of a comparison, you should include the comparison operation between parentheses. Here is an example:

using System;

class NewProject
{
	static void Main()
	{
		int Value1 = 15;
		int Value2 = 24;

		Console.Write("Value 1 = ");
		Console.WriteLine(Value1);
		Console.Write("Value 2 = ");
		Console.WriteLine(Value2);
		Console.Write("Comparison of Value1 == 15 produces ");
		Console.WriteLine(Value1 == 15);
	}
}

This would produce:

Value 1 = 15
Value 2 = 24
Comparison of Value1 == 15 produces True

It is important to make a distinction between the assignment "=" and the logical equality operator "==". The first is used to give a new value to a variable, as in Number = 244. The operand on the left side of = must always be a variable and never a constant. The == operator is never used to assign a value; this would cause an error. The == operator is used only to compare to values. The operands on both sides of == can be variables, constants, or one can be a variable while the other is a constant. If you use one operator in place of the other, you would receive an error when you compile the program.

The Logical Not Operator !

When a variable is declared and receives a value (this could be done through initialization or a change of value) in a program, it becomes alive. It can then participate in any necessary operation. The compiler keeps track of every variable that exists in the program being processed. When a variable is not being used or is not available for processing (in visual programming, it would be considered as disabled) to make a variable (temporarily) unusable, you can nullify its value. C# considers that a variable whose value is null is stern. To render a variable unavailable during the evolution of a program, apply the logical not operator which is !. Its syntax is:

!Value

There are two main ways you can use the logical not operator. As we will learn when studying conditional statements, the most classic way of using the logical not operator is to check the state of a variable.

To nullify a variable, you can write the exclamation point to its left. Here is an example:

using System;

class NewProject
{
	static void Main()
	{
		bool HasAirCondition = true;
		bool DoesIt;

		Console.Write("HasAirCondition = ");
		Console.WriteLine(HasAirCondition);
		DoesIt = !HasAirCondition;
		Console.Write("DoesIt          = ");
		Console.WriteLine(DoesIt);
	}
}

This would produce:

HasAirCondition = True
DoesIt          = False

When a variable holds a value, it is "alive". To make it not available, you can "not" it. When a variable has been "notted", its logical value has changed. If the logical value was true, which is 1, it would be changed to false, which is 0. Therefore, you can inverse the logical value of a variable by "notting" or not "notting" it.

The Inequality Operator !=

As opposed to Equality, C# provides another operator used to compare two values for inequality. This operation uses a combination of equality and logical not operators. It combines the logical not ! and a simplified == to produce !=. Its syntax is:

Value1 != Value2

The != is a binary operator (like all logical operator 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 a true or positive value. Otherwise, the comparison renders false or a null value:

Here is an example:

using System;

class NewProject
{
	static void Main()
	{
		int Value1 = 212;
		int Value2 = -46;
		bool Value3 = (Value1 != Value2);

		Console.Write("Value1 = ");
		Console.WriteLine(Value1);
		Console.Write("Value2 = ");
		Console.WriteLine(Value2);
		Console.Write("Value3 = ");
		Console.Write(Value3);
		Console.WriteLine();
	}
}

The inequality is obviously the opposite of the equality.

The Comparison for a Lower Value <

To find out whether one value is lower than another, use the < operator. Its syntax 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 Variable1 is lower than that of Variable2, the comparison produces a true or positive result.

Flowchart: Less Than

Here is an example:

using System;

class NewProject
{
	static void Main()
	{
		int Value1 = 15;
		bool Value2 = (Value1 < 24);

		Console.Write("Value 1 = ");
		Console.WriteLine(Value1);	
		Console.Write("Value 2 = ");
		Console.WriteLine(Value2);
		Console.WriteLine();
	}
}

This would produce:

Value 1 = 15
Value 2 = True

Combining Equality and Lower Value <=

The previous two operations can be combined to compare two values. This allows you to know if two values are the same or if the first is less than the second. The operator used is <= and its syntax is:

Value1 <= Value2

The <= operation performs a comparison as any of the last two. If both Value1 and VBalue2 hold the same value, result is true or positive. 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

Here is an example:

using System;

class NewProject
{
	static void Main()
	{
		int Value1 = 15;
		bool Value2 = (Value1 <= 24);

		Console.Write("Value 1 = ");
		Console.WriteLine(Value1);	
		Console.Write("Value 2 = ");
		Console.WriteLine(Value2);
		Console.WriteLine();
	}
}

This would produce:

Value 1 = 15
Value 2 = True

The Comparison for a Greater Value >

When two values of the same type are distinct, one of them is usually higher than the other. C# provides a logical operator that allows you to find out if one of two values is greater than the other. The operator used for this operation uses the > symbol. Its syntax 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 or positive value . Otherwise, the comparison renders false or null:

Greater Than

 

The Greater Than or Equal Operator >=

The greater than or the equality operators can be combined to produce an operator as follows: >=. This is the "greater than or equal to" operator. Its syntax 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 or positive value. If the value of the left operand is greater than that of the right operand,, the comparison produces true or positive also. If the value of the left operand is strictly less than the value of the right operand, the comparison produces a false or null result:

Flowchart: Greater Than Or Equal To

Here is a summary table of the logical operators we have studied:

 

Operator Meaning Example Opposite
== Equality to a == b !=
!= Not equal to 12 != 7 ==
< Less than 25 < 84 >=
<= Less than or equal to Cab <= Tab >
> Greater than 248 > 55 <=
>= Greater than or equal to Val1 >= Val2 <
 

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