...

/

Logical and Assignment Operators

Logical and Assignment Operators

Learn about different logical and assignment operators in Solidity.

We'll cover the following...

In this lesson, we’ll explore logical and assignment operators. By understanding logical operators, we’ll learn how to create complex conditions by combining boolean values. Simultaneously, we’ll learn assignment operators, simplifying the process of modifying variables.

Logical operators

These operators are used to pair two or more conditions together. The following logical operators are supported by Solidity:

  • AND (&&): This returns true if both criteria are true and false if just one of them is true.

  • OR (||): This returns true when one or both criteria are met and false when both are not.

  • NOT (!): This returns true if the condition is not met. Otherwise, false.

The following code example demonstrates the use of logical operators in Solidity:

Press + to interact
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract OperatorDemo{
// Initializing variables
bool public first = true;
bool public second = false;
// Initailize variable with the AND operator result
bool public and = first&&second;
// Initailize variable with the OR operator result
bool public or = first||second;
// Initailize variable with the NOT operator result
bool public not = !second;
}
  • Line 4: ...