From the simplest operation alphanumeric (+), the plus sign (+) is a two-dollar operator-that is, the plus sign joins only two numbers and never joins the third or more.
Therefore, "1 plus 2 plus 3" is stated in the computer as:
(1 + 2) + 3/A or
1 + (2 + 3)//b Although we usually write 1 + 2 + 3, it does not mean that it is equivalent to the 1+2+3 in our math.
So what does the 1+2+3 in mathematics mean by a or B?
If the computer's evaluation is left-bound, then the expression is equivalent to the first A; if it is right, then the expression is equivalent to the second type B.
1 + 2 + 3 The simple understanding is "put 1, 2, 3 together", and indeed, in the mathematics we are exposed to, we add up three numbers. But in programming languages, it's not just that.
As mentioned earlier, the + number cannot operate three or more digits, and only two digits can participate in the addition operation.
By the way, the plus and minus signs are unary operators, and although they are the same as the two-dollar operator plus and minus the same notation, they are different, so don't assume that +4 is equivalent to 0+4, but they're not equivalent.
+4 is an integer, but the 0+4 is an additive expression, and the expression evaluates to just +4.
In Java, we can write short a = +4, but when we write short a = 0 + 4 o'clock, a warning is generated.
There is one other example, which is 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 Neumann architecture, there's a side effect-I used to call those "side effects" when the computer's computational process is not the same as the programmer's brain thinking process (though it's not written in the book, but I always think so), you thought it would be, As a result, the computer does not do so, I call him the side effect.
If you look at the previous statement and expression, this is understandable:
1 + 2 is an expression whose return value is 3. The return value of this expression participates in another expression 3 + 3, and finally results in 6.
We use the statement (Statement) to rewrite the code:
Calculate 1 + 2 + 3
var a = 1 + 2;
var B = A + 3; if we evaluate this expression in Lisp, there is no side effect.
(+ (+ 1 2) 3) If you have not yet understood, or this example is too specific, then we change a
5 > 4 > 3 in Mathematics, the value of this formula is true. When we write this code in C, it returns true false.
The reason is the same as above, the greater-than sign (>) is a two-dollar operation, it cannot directly compare three numbers, 5 > 4 returns True, and when compared with true and 3, true is converted to 1, which is 1 > 3, and the final result is false.
All in all, it's back to the point of "statement and expression": every expression in a programming language has a value.
The operators in the programming language and the arithmetic operators in mathematics are the same, but they are not equal. When you're writing a program, write it to someone, and when you debug the program, learn to think about the meaning of the code in a computer way.
I'm used to understanding operators as functions, like 2 + 5 is actually add (2, 5) or 2.add (5). I would secretly tell you that "in fact, many languages are doing this."
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service