Open In App

Logical Operators in Solidity

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Logical Operators are used to combining two or more conditions. Solidity has the following types of logical operators:

  1. Logical AND: Logical AND takes two operands and gives the valid Boolean result. The logical AND operator evaluates to true when all the operands are true (non-zero) otherwise false(zero). It is denoted by &&.
  2. Logical OR: Logical OR takes two operands and gives the valid Boolean result. The logical OR operator evaluates to true when at least one of the operands is true (non-zero) or otherwise false(zero). It is denoted by ||.
  3. Logical NOT: Logical NOT takes one operand and gives its complement as the result. The logical NOT operator evaluates to true (non-zero) if the operand is false (zero) and vice-versa. It is denoted by !.
Operation  Denotation Description
Logical AND

&&

It evaluates to true when all the operands are true (non-zero) otherwise false (zero)
Logical OR

||

It evaluates to true when at least one of the operands is true (non-zero) otherwise false (zero)
Logical NOT

 !

It evaluates to true (non-zero) if the operand is false (zero) and vice-versa

These logical operators are used to evaluate conditions in different situations.

Below is the Solidity program to implement the Logical Operators:

Solidity




// Solidity program to implement 
// the Logical Operators
pragma solidity ^0.5.0;
  
contract logical {
    
  function logicaloperator (bool a , bool b) public pure returns (bool, bool, bool) {
    // Logical AND 
    bool logicaland = a && b;
      
    // Logical OR
    bool logicalor = a || b;
      
    // Logical NOT
    bool logicalnot = !b;
      
    return (logicaland, logicalor, logicalnot);
  }
}


Output: Let us have two Boolean variables a and b.

Case 1: When a is true and b is false.

a is true and b is false

 

Case 2: When a is false and b is false.

a is false and b is false

 

Case 3: When a is true and b is true.

a is true and b is true

 

Case 4: When a is false and b is true.

a is false and b is true

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads