Comparison Operators in Solidity
Comparison Operators are used to compare two values. Solidity has the following types of comparison operators:
- Greater than: Greater than operator compares two operands. It evaluates to true when the left operand is greater than the right operand otherwise false. It is denoted by >.
- Lesser than: Lesser than operator compares two operands. It evaluates to true when the left operand is lesser than the right operand otherwise false. It is denoted by <.
- Greater than equal to: Greater than equal to operator compares two operands. It evaluates to true when the left operand is greater than or equal to the right operand otherwise false. It is denoted by >=.
- Lesser than equal to: Lesser than equal to operator compares two operands. It evaluates to true when the left operand is lesser than or equal to the right operand otherwise false. It is denoted by <=.
- Equal: An equal operator compares two operands. It evaluates to true if both the operands are equal otherwise false. It is denoted by ==.
- Not equal: Not equal operator compares two operands. It evaluates to true if both the operands are unequal otherwise false. It is denoted by !=.
Operator | Denotation | Description |
---|---|---|
Greater than |
> |
Evaluates to true if the left operand is greater than the right operand. |
Lesser than |
< |
Evaluates to true if the left operand is lesser than the right operand. |
Greater than equal to |
>= |
Evaluates to true if the left operand is greater than or equal to the right operand. |
Lesser than equal to |
<= |
Evaluates to true if the left operand is less than or equal to the right operand. |
Equal |
== |
Evaluates to true if the left operand is equal to the right operand. |
Not equal |
!= |
Evaluates to true if the left operand and right operand are unequal. |
Below is the Solidity program to implement Comparison Operators:
Solidity
// Solidity program to implement // Comparison Operator pragma solidity ^0.5.0; contract Comparison { function comparisonoper (uint a, uint b) public pure returns ( bool , bool , bool , bool , bool , bool ) { // Greater than Comparison Operator bool gt = a > b; // Lesser than Comparison Operator bool lt = a < b; // Greater than equal to Comparison Operator bool gtet = a >= b; // Lesser than equal to Comparison Operator bool ltet = a <= b; // Equal Comparison Operator bool e = a == b; // Not equal Comparison Operator bool ne = a != b; return (gt, lt, gtet, ltet, e, ne); } } |
Output:
Contact Us