Logical Operators in Solidity
Last Updated :
21 Mar, 2023
Logical Operators are used to combining two or more conditions. Solidity has the following types of logical operators:
- 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 &&.
- 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 ||.
- 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
pragma solidity ^0.5.0;
contract logical {
function logicaloperator ( bool a , bool b) public pure returns ( bool , bool , bool ) {
bool logicaland = a && b;
bool logicalor = a || b;
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.
Case 2: When a is false and b is false.
Case 3: When a is true and b is true.
Case 4: When a is false and b is true.
Share your thoughts in the comments
Please Login to comment...