Skip to main content

Command Palette

Search for a command to run...

Logical Operator

Published
2 min readView as Markdown
Logical Operator

Logical Operator

Solidity provides several logical operators that can be used to perform logical operations on boolean values. Here are the logical operators in Solidity:

  • && (logical AND): Returns true if both operands are true.

  • || (logical OR): Returns true if at least one of the operands is true.

  • ! (logical NOT): Returns the opposite boolean value of the operand.

  • By default we don't any exor operator in solidity.

  • The order of logical operators in Solidity is determined by operator precedence, which is the order in which operators are evaluated in an expression.

  • In Solidity, logical NOT (!) has the highest precedence, followed by logical AND (&&), and then logical OR (||).Here's an example that demonstrates how to use logical operators in Solidity:

arduinoCopy codebool a = true;
bool b = false;

bool c = a && b; // false, because both operands are not true
bool d = a || b; // true, because at least one operand is true
bool e = !a;     // false, because the opposite of true is false

In this example, we have two boolean variables a and b, and we use the logical operators &&, ||, and ! to perform logical operations on them. The result of the logical operations are stored in the variables c, d, and e, respectively.

It's important to note that logical operators in Solidity short-circuit, which means that if the value of the expression can be determined from the first operand, the second operand will not be evaluated. This can have an impact on the gas cost of your contract, so it's important to consider this when writing your code.

More from this blog