# Comparison Operators aka Relational Operators

## Comparison Operators or Relational Operators

Just like Arithmetic Operators, we also have comparison operators or Relational Operators. These are just two names of the same thing. The comparison operators compare two operands and return a boolean value (True or False).

Just like you see two posters and compare them if they are identical. The answer will come out of your mouth is Yes or No. The comparison operators do the same thing by giving you a result as True or False. Let’s see how many comparison operators do we have in C# by Microsoft.

1. == (Equal)
2. != (Not Equal)
3. > (Greater than)
4. < (Less than)
5. >= (Greater than Equal)
6. <= (Less than Equal)

### Equal Operator (==)

Equal Operator is to compare if two operands are equal or not it gives you a boolean result, true or false. Let me show you a program with an equal operator.

``````using System;
namespace Comparison_Operators
{
class Program
{
static void Main(string[] args)
{
var a = 10;
var b = 10;
if (a==b)
{
Console.WriteLine("a and b are equal");
}
else
{
Console.WriteLine("a and b are not equal");
}
}
}
}
``````

#### Explanation

In the above program, you can see a and b are being compared using the comparison equal (==) operator. There is one more operator being used which we have not discussed yet, the conditional operator if. We will discuss this operator in a future article. For now, you can think of a conditional operator as a checker of a condition, if the condition is true then a particular piece of code will be executed. if the condition is false then the other piece of code will be executed.

In this example program, the Equal operator is giving true or false to the conditional operator based on the comparison of a and b. Then the conditional operator is executing the code based on true or false.

### Not Equal Operator (!=)

This operator is just like an equal operator but it checks the condition in a different way. It checks if a certain condition is not true. Basically, it checks the reverse condition of the equal operator.

``````using System;
namespace Comparison_Operators
{
class Program
{
static void Main(string[] args)
{
var a = 10;
var b = 10;
if (a!=b)
{
Console.WriteLine("a and b are not equal");
}
else
{
Console.WriteLine("a and b are equal");
}
}
}
}
``````

#### Explanation

As you can see in the program, the program is exactly the same as an equal operator program but this time we have checked if a and b are not equal. Accordingly, the conditional operator is printing the statement.

### Greater Than Operator (>)

This operator also takes two operands and checks which one is bigger.

``````using System;
namespace Comparison_Operators
{
class Program
{
static void Main(string[] args)
{
var a = 10;
var b = 10;
if (a>b)
{
Console.WriteLine("a is bigger than b");
}
if (b>a)
{
Console.WriteLine("b is bigger than a");
}
else
{
Console.WriteLine("a and b are equal");
}
}
}
}
``````

#### Program Output

As you can see in the above program. Greater than operator is checking if a is greater than b or not. It has found a is not greater than b. Then is has checked b is greater than a or not. It has found b is not greater than a. It simply means both operands are equal. This is why the conditional operator printed the third statement on the console screen because both conditions it has checked were false.

### Less Than Operator (<)

This operator is just the opposite of greater than the operator. It will check if a is smaller than b then it will return true otherwise it will return false. Let’s see a program.

``````using System;
namespace Comparison_Operators
{
class Program
{
static void Main(string[] args)
{
var a = 10;
var b = 10;
if (a<b)
{
Console.WriteLine("a is smaller than b");
}
if (b<a)
{
Console.WriteLine("b is smaller than a");
}
else
{
Console.WriteLine("a and b are equal");
}
}
}
}
``````

#### Program Output

In the above program, the less-than operator is checking if a is smaller than b, it found a is not smaller than b so it returned False. Then it checked it b is smaller than a, it found b is not smaller than a. Because both are equal so first two conditions were false. It simply means the third condition will be true that both are equal.

### Greater Than Equal Operator (>=)

Now, this operator is a bit advanced, it checks two conditions at the same time. The two conditions are mentioned below.

1. a is bigger than b.
2. a is equal to b.

If one of the conditions is true then the Greater Than Equal Operator will return true. if both of the conditions are false then it will return false. To make this clear, I will show you the same program with different values of a and you will understand the concept of Greater than equal (>=).

#### Example 1

``````using System;
namespace Comparison_Operators
{
class Program
{
static void Main(string[] args)
{
var a = 10;
var b = 10;
if (a>=b)
{
Console.WriteLine("a is bigger than b or equal to b");
}
else
{
Console.WriteLine("a is smaller than b");
}
}
}
}
``````

#### Example 2

``````using System;
namespace Comparison_Operators
{
class Program
{
static void Main(string[] args)
{
var a = 11;
var b = 10;
if (a >= b)
{
Console.WriteLine("a is bigger than b or equal to b");
}
else
{
Console.WriteLine("a is smaller than b");
}
}
}
}
``````

#### Example 3

``````using System;
namespace Comparison_Operators
{
class Program
{
static void Main(string[] args)
{
var a = 9;
var b = 10;
if (a >= b)
{
Console.WriteLine("a is bigger than b or equal to b");
}
else
{
Console.WriteLine("a is smaller than b");
}
}
}
}
``````

#### Program Output

See all these three programs where I have just changed the value of a to bigger than b and equal to b, and program output did not change. When I modified the value of a to less than b. In that circumstance, the output changed because the Greater than equal operator returned true if the first operand is bigger or equal than the second operand. When I set the value of a less than b, in that situation the operator returned false, and the second block of code got executed which says a is smaller than b.

### Less Than Equal Operator (<=)

Just like greater than equal, less than equal also checks two conditions. It checks if the first operand is smaller than the second operand or the first operand is equal to the second operand. If any of the two situations is true, it will return true otherwise it will return false. Let’s understand this with examples.

#### Example 1

``````using System;
namespace Comparison_Operators
{
class Program
{
static void Main(string[] args)
{
var a = 9;
var b = 10;
if (a <= b)
{
Console.WriteLine("a is smaller than b or equal to b");
}
else
{
Console.WriteLine("a is bigger than b");
}
}
}
}
``````

#### Example 2

``````using System;
namespace Comparison_Operators
{
class Program
{
static void Main(string[] args)
{
var a = 10;
var b = 10;
if (a <= b)
{
Console.WriteLine("a is smaller than b or equal to b");
}
else
{
Console.WriteLine("a is bigger than b");
}
}
}
}
``````

#### Example 3

``````using System;
namespace Comparison_Operators
{
class Program
{
static void Main(string[] args)
{
var a = 11;
var b = 10;
if (a <= b)
{
Console.WriteLine("a is smaller than b or equal to b");
}
else
{
Console.WriteLine("a is bigger than b");
}
}
}
}
``````

#### Output

See all three example programs and notice the value of a is set to 9 and 11 from 10. The Less Than Equal Operator is checking if the value of a is smaller than b or equal to b. In the third example, it found the first two conditions a is less than b or a is equal to b are false. That simply means its third condition is true which is a is bigger than b.

## A Suggestion

All of these operators are very easy to understand if you will take some time and understand the programs. Don’t be in hurry and understand the example programs carefully. In case of any confusion, feel free to comment.

Take Care Guys. Stay Safe Stay Healthy!