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
14
15
16
17
18
19
20
21
|
using
System;
namespace
HelloWorld
{
class
Program
{
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.ReadLine();
}
}
}
|
The output of the above code snipet is:
x > y : True
x < y : False
x >= y : True
x <= y : False
x == y : False
x != y : True
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 outputs true, because, indeed, 9 is greater than 3.
The concepts explained in this lesson are also shown visually as part of the following video: