Relational operators are used to check the relationship between two operands. If the relationship is true the result will be true
, otherwise it will result in false
.
Relational operators are used in decision making and loops.
Operator | Operator Name | Example |
---|---|---|
== | Equal to | 6 == 4 evaluates to false |
> | Greater than | 3 > -1 evaluates to true |
< | Less than | 5 < 3 evaluates to false |
>= | Greater than or equal to | 4 >= 4 evaluates to true |
<= | Less than or equal to | 5 <= 3 evaluates to false |
!= | Not equal to | 10 != 2 evaluates to true |
Example: Relational Operators
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 26 27 |
using System; namespace Operator { class RelationalOperator { public static void Main(string[] args) { bool result; int firstNumber = 10, 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); } } } |