In this section, we're going to take a look at the different operators available in Solidity. Doing so is going to help you better understand the logical operations that you can write into your contracts to implement business logic.
The first three operators that we will cover are as follows:
- Assignment
- Equality
- Inequality
After just getting started with programming, you may find these confusing; however, a minute spent now is going to save you hours of frustration down the road.
Assignment is used to assign a value to a variable. For example we can assign the variable foo with a value of bar, as seen in the following code snippet:
string foo = "bar";
foo == bar;
foo != bar;
The next operator is equality, and we use it to determine whether one variable is equal to another. In the preceding example, we're checking to see whether the value of the variable foo is equal to the string bar, and in our case, it is, so this expression would evaluate to true.
And finally we have inequality, which is the exact opposite of equality. So, in the third line of the previous example, we're checking to see if foo is not equal to bar, but it is equal, so this expression is going to evaluate to false.
We also have a set of comparison operators. We can use these to determine if one value is greater than (>), greater than or equal to (>=), less than (<), or less than or equal to (<=) the other value. These operators work similarly to the equality and inequality operators we just looked at.
We also have some shorthand operators that'll save you time when you're writing code. Some of them are as follows:
- +=: This is the addition shorthand operator
- -=: This is the subtraction shorthand operator
- *=: This is the multiplication shorthand operator
- /=: This is the division shorthand operator
- %=: This is the remainder shorthand operator
- |=: This is the logical and shorthand operator
- &=: This is the logical or shorthand operator
We can also use the a++ and a-- operators to increment or decrement a counter by 1. When executing one of these though, the expression will return the value of a prior to the change, so if a = 1, and we execute the a++ operator, the expression returns an output of 1, but the value of a is now 2.
There are also ++a and --a, which do the same thing. They increment or decrement by 1, but they return the actual value after the change. So, if a = 1, and we execute ++a, the expression will return the new value of a , which is 2.
We can use the delete operator to assign the variable with the initial value for its type. For example, if foo is an integer, executing delete foo will set foo to a value of 0.
When an operator's applied to different types, the compiler tries to implicitly convert one of the operands to the type of the other. Implicit conversion is possible if it makes sense semantically, and no information is lost in the conversion. For example, an 8-bit unsigned integer (uint8) is convertible to a uint16, a uint28, or uint256, but an 8-bit integer (int8) is not convertible to an unsigned 256 bit integer, because a uint256 can't hold negative numbers.
Now that we have an understanding of operators and variables, and how to use them in Solidity, let's take a look at some practical examples, by using them to create business logic in our contracts.