Let's start with the simplest operator, the plus sign (+). The plus sign (+) is a binary operator - that is to say, the plus sign only connects two numbers, never the third or more numbers. Connected.
Therefore, "1 plus 2 plus 3" is expressed in the computer as:
(1 + 2) + 3 // aor
1 + (2 + 3) // bAlthough we usually write it as 1 + 2 + 3, it does not mean that it is equivalent to 1+2+3 in our mathematics.
So does 1+2+3 in mathematics represent a or b?
If the computer's evaluation is left associative, then this expression is equivalent to the first type a; if it is right associative, then this expression is equivalent to the second type b.
The simple understanding of 1 + 2 + 3 is "add 1, 2, and 3 together". Indeed, in the mathematics we are exposed to, it is to add three numbers. But in programming languages, it's not just that.
As mentioned before, the + sign cannot operate with three or more numbers, and can only participate in addition operations with two numbers.
By the way, plus and minus are unary operators. Although they use the same symbols as the binary operators addition and subtraction, they are different, so don’t take it for granted that +4 is equivalent to 0+4. , in fact they are not equivalent,
+4 is an integer, but 0+4 is an addition expression, which evaluates to exactly +4.
In java, we can write short a = +4, but when we write short a = 0 + 4 a warning is generated.
There is another example, also about short,
short b = 1;
short b = b + 4; // warning
short b += 4; // No warning. So how does 1 + 2 + 3 work? In the programming language of the von Neumann architecture, there is a side effect - I am used to calling those "when the computing process of the computer is different from the thinking process of the programmer's brain, it is called side effect" (although it is not written like this in the book) Yes, but I have always thought so), originally you thought it would be like this, but it turns out that the computer does not do this. I call it a side effect.
If you have read the previous "Statements and Expressions", this can be understood like this:
1 + 2 is an expression whose return value is 3. The return value of this expression is then added to another expression 3 + 3, and the final result is 6.
We rewrite this code using Statement:
// Calculate 1 + 2 + 3
var a = 1 + 2;
var b = a + 3;If we evaluate this expression in lisp, there are no side effects.
(+ (+ 1 2) 3) If you don’t understand it yet, or this example is too special, let’s change it to another one.
5 > 4 > 3 In mathematics, this equation evaluates to true. When we write this code in C language, it returns false indeed.
The reason is the same as above. The greater than sign (>) is a binary operation. It cannot directly compare three numbers. The result returned by 5 > 4 is true. When true is compared with 3, true is converted to 1, which is 1 > 3, the final result is naturally false.
In short, we return to the point of view in the "Statements and Expressions" article: every expression in a programming language has a value.
Although operators in programming languages are the same as operators in mathematics, they are not equivalent. When you write a program, write it for people to read; when you debug a program, you must learn to think about the meaning of the code in a computer way.
I am used to understanding operators as functions. For example, 2 + 5 is actually add(2, 5) or 2.add(5). Am I going to tell you secretly, "In fact, many languages do this."