Monday, June 23, 2025 18:57

Table of contents >> Introduction > Comparison operators

Comparison operators

Another kind of operators we have enumerated are the comparison operators. As their name suggest, comparison operators are used to compare operands. There are 6 comparison operators:

>    greater than
<    less than 
>=   greater than or equal to
<=   less than or equal to
==   equality
!=   difference

All of the above operators return a Boolean value (true or false). Here is an example of using comparison operators:

static void Main(string[] args)
{
    int x = 9, y = 3;

    Console.WriteLine("x > y : " + (x > y)); // True 
    Console.WriteLine("x < y : " + (x < y)); // False 
    Console.WriteLine("x >= y : " + (x >= y)); // True 
    Console.WriteLine("x <= y : " + (x <= y)); // False 
    Console.WriteLine("x == y : " + (x == y)); // False 
    Console.WriteLine("x != y : " + (x != y)); // True

    Console.Read();
}

The output of the above code snipet is

comparison operators

In the above example, we declared two integer variables, x and y and we assigned them the values of 9 and 3. On the next line, we used the comparison operator “greater than” to compare if x is greater than y, which output True, because, indeed, 9 is greater than 3.

The concepts explained in this lesson are also shown visually as part of the following video:

Tags: ,

Leave a Reply