It distinguishes tasks based on (get) which operator you use. When the code is parsed, the parser / compiler finds the operators and decides what to do with things around it. If + is used for both addition and concatenation, it will depend on the arguments provided to it. The language decides what to do when it is mixed, for example, if "4" + 5 is equal to "45" or 9.
In addition, operators have priority. That is, some operators are evaluated before others. Similar to the order of operations you study in math. For example, the operators + and - usually have a lower priority than the operators * and / (multiplication and division) (that is, the operations of multiplication will be performed before the operations of addition). That is why 4 * 5 + 2 will be 22 instead of 28. You can find the operator priority table here. This is for the C language, but it should be similar for most other languages.
There is also associativity of operators. This is (basically) the order in which operators of the same type are evaluated. Most of the ones you're used to will remain associative. For instance:
4 * 5 * 6 * 7
it is left associative, which means the following:
((4 * 5) * 6) * 7
if there are two or more operators with the same priority "in the line" (as in the example), then they will be evaluated from left to right. That is, first 4 * 5, then 20 * 6, then 120 * 7. An example of the correct associative operator is the exponent operator:
4 ^ 5 ^ 6 ^ 7
It is getting
4 ^ (5 ^ (6 ^ 7))
They are evaluated from right to left. If the exponent operator remained associative, this would give the wrong answer.
Recall associativity as "which side the brackets continue." they are left for the left associative and right for the right associative.
That same link includes associativity for each operator. Again, this is for the C language, but it should be nearly identical for most other languages that you will use.
source share