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:
1 2 3 4 5 6 |
> 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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
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: comparison operator, operators