643
The c# relational operators will return true only when the defined operands relationship becomes true. Otherwise, it will return false.
Relational operators are used in decision making and loops.
The following table lists the different types of operators available in c# relational operators.
Operator | Name | Description | Example |
---|---|---|---|
== | Equal to | It compares two operands, and it returns true if both are the same. | a == b (false) |
> | Greater than | It compares whether the left operand greater than the right operand or not and returns true if it is satisfied. | a > b (true) |
< | Less than | It compares whether the left operand less than the right operand or not and returns true if it is satisfied. | a < b (false) |
>= | Greater than or Equal to | It compares whether the left operand greater than or equal to the right operand or not and returns true if it is satisfied. | a >= b (true) |
<= | Less than or Equal to | It compares whether the left operand less than or equal to the right operand or not and returns true if it is satisfied. | a <= b (false) |
!= | Not Equal to | It checks whether two operand values equal or not and return true if values are not equal. | a != b (true) |
Example
using System; public class Program { public static void Main() { bool result; int firstNumber = 30, secondNumber = 20; result = (firstNumber==secondNumber); Console.WriteLine("{0} == {1} returns {2}",firstNumber, secondNumber, result); result = (firstNumber > secondNumber); Console.WriteLine("{0} > {1} returns {2}",firstNumber, secondNumber, result); result = (firstNumber < secondNumber); Console.WriteLine("{0} < {1} returns {2}",firstNumber, secondNumber, result); result = (firstNumber >= secondNumber); Console.WriteLine("{0} >= {1} returns {2}",firstNumber, secondNumber, result); result = (firstNumber <= secondNumber); Console.WriteLine("{0} <= {1} returns {2}",firstNumber, secondNumber, result); result = (firstNumber != secondNumber); Console.WriteLine("{0} != {1} returns {2}",firstNumber, secondNumber, result); } }
You should see something like this
30 == 20 returns False 30 > 20 returns True 30 < 20 returns False 30 >= 20 returns True 30 <= 20 returns False 30 != 20 returns True