What are the relational operators in C#
Description
The relational operators are as follows:
Operator | Meaning |
---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example
using System; //from w w w.j a v a 2 s. co m
class Example {
public static void Main() {
int i, j;
i = 10;
j = 11;
if(i < j)
Console.WriteLine("i < j");
if(i <= j)
Console.WriteLine("i <= j");
if(i != j)
Console.WriteLine("i != j");
if(i == j)
Console.WriteLine("this won't execute");
if(i >= j)
Console.WriteLine("this won't execute");
if(i > j)
Console.WriteLine("this won't execute");
}
}
The code above generates the following result.
Example 2
/*www . j av a 2s . c o m*/
using System;
class MainClass
{
static void Main(string[] args)
{
int a = 10, b = 20, c = 30;
if (a < 15 && b < 20)
c = 10;
Console.WriteLine(c);
if (a < 15 || b < 20)
c = 15;
Console.WriteLine(c);
if (!(a == 15))
c = 25;
Console.WriteLine(c);
}
}
The code above generates the following result.