Open In App

Comparison Operators in Solidity

Improve
Improve
Like Article
Like
Save
Share
Report

Comparison Operators are used to compare two values. Solidity has the following types of comparison operators:

  1. 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 >.
  2. 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 <.
  3. 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 >=.
  4. 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 <=.
  5. Equal: An equal operator compares two operands. It evaluates to true if both the operands are equal otherwise false. It is denoted by ==.
  6. 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:

Comparison Operator

 



Last Updated : 21 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads