Operators are the glue holding many of your coding concepts together. You use them to manipulate strings, fetch data from databases, and even update variables. With so many uses and such a broad set of features, it’s no wonder that every programmer needs to know how to use operators effectively. Characters or symbols that perform operations on one or more operands are called operators. Here, we’ll go over the basics and provide some helpful references so that you can start practicing.
What makes an operator an operator?
There are many different kinds of operators in programming, each of which has a subtly different meaning and object that it operates on. Here, I’ll talk about the five most widely used and widely misunderstood operators. When you understand them, you’ll know about many hidden gems in your programming toolbox, and be much better prepared for almost any task programmers face.
1. The Content of An Operator
Operator class names are typically named using camel case, which means each letter of the operator name is capitalized (e.g. a-b-c). Therefore, operator names are generally shorter than most functions and methods. If a particular operator is overloaded, it may have more than one class name, but the same function will have the same name.
2. Meaning of An Operator
You must think about the meaning of an operator when you use it: If a program uses a particular operator to do one thing, and then it’s used in the same program again in a different place, what does that mean? When you think about what an operator is doing in different contexts, you get a better handle on how it affects your code. For example:
A1 = A + B
This is probably pretty obvious. Say this operation was done twice. First, you put two numbers together: A + B = 5. Then say you put two numbers together again with this operation: A + B = C + 7 . Which number is bigger? Now multiply both numbers by 3: A + B = C + 7 * 3 = 36.
3. Operator Equality
Equivalence is a very important concept to understand. The concept of operator equivalence states that two operators are the same if their behavior is identical except for their operands.
For example, use this code:
A = 5 * 6 * B * C * D * E * F There are several different ways you could choose to write this, but the result would be the same: 28. If you add 10 then run it again, the result would be 28 as well on each run. 28 is the same thing as 10 + 18 + 11 + 16 + 9 + 6. They are equivalent.
Now, take this code: A = B * C – D / E The difference between the two examples is subtraction and division. One of the operators uses subtracts and divides, while the other uses multiplication and addition. In both examples, we can see that the operands don’t matter: All we care about is the operator used.
4. Operator Prefixes
An operator prefix is a character that is added to the start of an operator. This character indicates what type of operation will be performed, and it changes the meaning of the operator. Adding a prefix to an operator changes its meaning. For example:
5 * 6 + 7 means something different than 5 + * 6 + 7 . In fact, they are equivalent because they still equal 28. Now look at this code: 5 *+ 6 + 7 The plus sign is optional after an integer in Swift, but you will never write * plus or any other non-operator characters when you use this expression. 5 * 6 + 7 is a way to add numbers.
It’s correct, but it’s not — 6 * 7 is an error that produces the following error message:
Equal initializers must have the same number of arguments, but these initializers do not.(…/5 * 6 + 7)
The addition operator will always be used with a multiplication operator if both of them are present. In this case, Swift doesn’t know how to do arithmetic by itself. You can think of the operators as being rather like mathematical statements that would tell the computer how to solve problems in code (i.e. addition or multiplication). The expression above has two operators doing what we want: Addition and multiplication.
I can add or multiply, but I usually know what I’m doing, and so should you.
The same holds true for many other operators:
-5 – 7 == -8 – 5 + 7 == -11
5 * 6 == 28
5 + * 6 == 36
* 6 + * 5 == 36
5. Operand Type Conversions
Convertors (or coercion) are functions that are used to convert one type of object to another. There are two different types of coercion operators: Implicit and Explicit. An implicit operator coerces its operands without your knowledge; it’s the default behavior when an operator isn’t specified.