Since we learn programming, we must be troubled by the increase and decrease. That is to say, stupid people are always confused a++and++a But when I see some topics, I can't tell them clearly, especially when the expression that needs to be calculated is longer and more confused, and even can only guess one answer. So today I will provide a better solution for this type of problem, which is not the best solution for the whole network, but also a basis In this book, you can change everything and answer 100 pairs of methods. Let me tell you in detail:
a + + and + + a
A + +: assign a value first and then add one;
++a: First add one, then assign a value;
// introduction var a = 1; console.log(a++);//1 //A is assigned a value directly, so the previous result is 1 //But at this time a has become 2, that is, a=2; //So in the next statement, a is 2, and a becomes 3 console.log(++a);//3
[the transfer of external chain pictures failed. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-ol0mwbhk-1595166520913) (C: users / silly / desktop / images)\ rumen.jpg ]
It may be a piece of cake for those who have already learned programming. Then let's look at an advanced problem.
//Advanced var b = 1; var c = b + ++b + b++;//5 //The first b is 1, the following ++b value is 2, //The last b++ is also 2, and the b at this time is b=3 after the assignment. console.log(c);//5
[External link image transfer failed, the source site may have an anti-leech mechanism, it is recommended to save the image and upload it directly (img-gStGAJUG-1595166520925)(C:\Users\Silly\Desktop\images\jinjie.jpg)]
According to the general calculation, most people should still be able to calculate the correct result, and then look at the questions that only change the order, and the result will change.
//Advanced deformation var b = 1; var c = b + b++ + ++b;//5 //The first b is still 1, and the second b++ is also 1 at this time, but //Note that b=2 at this time, so ++b=3 at this time, so the result is still 5 console.log(c);//5
Seeing this, many people will find a little different, so let's continue to look down and see what is a bit difficult. Let's use code examples to explain, so that the results will be clearer.
//Premium var a = 1; var b = a + a++ + (++a * a++ + 1) * a++ + 1; //When a=1, b=1 + 1 + (++a * a++ + 1) * a++ + 1; //(at this time a=2) //When a=2, b=2 + (3*a++ + 1)*a++ +1; (at this time a=3) //When a=3, b=2 + (3*3 +1)*a++ +1; (at this time because the parentheses //Because of a++ inside, a=4) //When a=4, b=2 + 10*4 + 1 = 43; (at this time because the last //For the sake of a++, a=5) console.log(a);//4 console.log(b);//33
[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-48BzonfA-1595166520932)(C:\Users\Silly\Desktop\images\gaoji.jpg)]
When I get here, it is estimated that I should pour a large piece of it. I feel that the answer is very strange. I just can't calculate the result. It's okay, I'll talk about the steps first. According to the order of the expressions from left to right, Then according to the above self-increase and self-decrease operation rules, calculate in turn, you can get the correct result. The detailed rules will be summarized after I list the final version, let's sell a small key first.
//Final version var a = 1; var b = a++ + 1 + ++a +(a = ++a *a++ + a++ + 1) * 1 + a++; //When a=1, b=1+1+ ++a +(a = ++a *a++ + a++ + 1) * 1 //+ a++; (at this time a=2) //When a=2, b=2+3+ +(a = ++a *a++ + a++ + 1)*1 + a++; //At this time a=3 //When a=3, b=5+(a = 4*a++ + a++ + 1)*1 + a++; (at this time a=4) //When a=4, b=5+(a = 4*4 + a++ + 1)*1 + a++; (at this time a=5) //When a=5, b=5+(a=16+ 5 +1)*1 + a++; (at this time a=22) //Note: Here a was originally the result 6, but because the value of a at this time //is assigned to a, so the value of a at this time is not 16, but 22. //When a=22, b=5+22+22 (the last a++ at this time makes the value of a 23 //But it is assigned to b with 22, so the final value of b is 49, not 50) console.log(a);//23 console.log(b);//49
[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-CtQ1yNam-1595166520944)(C:\Users\Silly\Desktop\images\finally.jpg)]
Is the final result very unexpected? If you are still doubting the final result, it means that you are not careful enough. Please check the thinking process carefully. Most people will write the result directly when a=6, without noticing the inside. A = ++a *a+++a+++1 is a pit In fact, the development of this topic is completely useless, but it is often used to test our understanding of the level of increasing and decreasing of this knowledge point Having said so much, let’s make a brief summary.
Expression results are processed from left to right
From left to right, the assignment symbol = the operator of the two expressions on the right
Judging the operation priority according to these two symbols (the priority with parentheses is increased)
Then calculate the value according to the operand that is incremented and decremented
a++ means that the current number participates in the operation with the value of a, but the operand +1
++a means that the current number participates in the operation with the value of a+1.
Calculate the result
According to the above summary, I believe that you must be able to have a clearer understanding of the accurate operation of self-increment and self-decrease. In fact, this kind of question is more likely to cause us to lose points carelessly. After all, development is rarely used. But no matter what , as a prospective developer, it is still very necessary to figure out this problem.