Relational Operators are used in a program to check the relationship between two operands and accordingly, it returns boolean values, true or false. These operators are used with loops and Decision-Making Statements during condition checking.
List of relational operators in C#:
Operator | Name | Example |
---|---|---|
== | Equal to. | A == B |
!= | Not equal. | A != B |
> | Greater than. | A > B |
< | Less than. | A < B |
>= | Greater than or equal to. | A >= B |
<= | Less than or equal to. | A <= B |
Example of Relational Operators in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | using System; namespace Operators { class Relational { static void Main(string[] args) { int a = 11; int b = 10; //Gives true and false value Console.WriteLine("'==' operator: {0}", a == b); Console.WriteLine("'<' operator: {0}", a < b); Console.WriteLine("'>' operator: {0}", a> b); Console.WriteLine("'!=' operator: {0}", a != b); /*Now if we change the value*/ a = 5; b = 20; Console.WriteLine("'<=' operator: {0}", a <= b); Console.WriteLine("'>=' operator: {0}", a>= b); } } } |
Output:
1 2 3 4 5 6 7 | '==' operator: False '<' operator: False '>' operator: True '!=' operator: True '<=' operator: True '>=' operator: False |