Hola, scholars! Today we’re talking about expressions and precedence rule. Don’t be afraid if you don’t know these terms! Because I’m sure you will know after reading this article. *Winky Face* Should we start?
After grasping the meaning of operators and operands from the picture, we need to understand that the operator evaluation order is determined by the associativity and precedence rules, which might differ for two different programming languages. Programmers are free to choose the order of operand evaluation, however, this does not guarantee that the answer to the expression will be the same…
Arithmetic Expressions — The expressions we use in school math are arithmetic operations. The characteristics of arithmetic operations in programming are also inspired by mathematics. Arithmetic expressions consist of operands, operators, parentheses, and function calls. An operator can be unary, binary, and ternary, meaning it requires single, two, and three operands respectively.
/*unary operator: */
/*binary operator: */
c = a+b;
/*ternary operator: */
c = k ? a:b;
You might not be familiar with the last expression. The value of the variable c is equal to a if k is true, otherwise, c is equal to b.
Operators can be prefix, infix, and postfix, and this division is detected by the location of the operator. Prefix means that the operator is at the beginning and postfix is at the end, but infix means that the operator is between 2 operands. For example, the “++” operator is both postfix and prefix, pre-increment and post-increment. The “&&” and “||” logical operators are infix:
b = true && false; // AND operator
c = false || true; // OR operator
Precedence (priority in importance)— The value of an expression depends on the order of the expression. Let’s analyze the following expression.
d = a + b * c;
Assume that the values of a, b, c are 2, 3, 4, respectively. If we evaluate from left to right, d will be equal to 20, if we evaluate from right to left, d will be equal to 14. Instead of evaluating this expression from left to right or right to left, the program will follow the rules of mathematics. So there is a hierarchy of evaluation priorities that most of us know from math. For example, multiplication takes precedence over addition. The operator has a role in defining the precedence rules for expression evaluation order. These rules can also be applied to imperative programming languages because the rules are based on mathematics.
In these languages, exponentiation has the highest precedence, followed by multiplication and division on the same level, followed by binary addition and subtraction on the same level.
In some languages, the use of unary addition and subtraction is legal, although there are some rules for its proper use. This operator can be seen either at the beginning or in the middle and should only be processed with parentheses. That is, if there is an operator before that and no parentheses are used, it is illegal.
d = a + (-b) * c; //legal
d = a + -b * c; //illegal
Let’s look at the following 3 expressions.
d = -a / b;
d = -a * b;
d = -a ** b; // is equal to -(a**b), not to (-a)**b.
The order of evaluation of the two operators has no effect on the value of the expression, but in the last one, it does. No matter what we do in the last expression, the negative sign remains outside the parentheses because exponentiation has higher precedence than a unary minus. It will be equivalent to -(a**b).
I hope what you read is instructive or reminiscent. Although programming is a science, it sometimes sounds mysterious, but there are compelling reasons behind why it is the way it is. Enjoy the reading, peace ✌🏼