I have never understood the difference between a ++ and ++ a. What should I do?

Source: Internet
Author: User
{Code ...}
I have never understood that the usage of a ++ and ++ a is confusing. First, I would like to ask if a ++ is equal to a + = 1. If not, why? Why does the value of $ a returned by the echo change? $ a remains unchanged. $ a = 5; $ B = $ a ++; echo $ B; echo $ a; there is another $ a = 5; $ B = $ a ++ $ a; echo $ B; echo $ a; why is the output $ B 11, $ a is 7? $ A ++ is 6? Then ++ $ a is 7, 6 + 7 is not 13? Why is it 11?

Reply content:
I have never understood that the usage of a ++ and ++ a is confusing. First, I would like to ask if a ++ is equal to a + = 1. If not, why? Why does the value of $ a returned by the echo change? $ a remains unchanged. $ a = 5; $ B = $ a ++; echo $ B; echo $ a; there is another $ a = 5; $ B = $ a ++ $ a; echo $ B; echo $ a; why is the output $ B 11, $ a is 7? $ A ++ is 6? Then ++ $ a is 7, 6 + 7 is not 13? Why is it 11?

There is such a secret
++ A values first and then the operation can be understood as the first + 1 operation.
A ++ values after calculation can be understood as "first calculation" and "+ 1"

Do you understand?

If yes

$ B = $ a ++ $; /* in three parts, the priority of ++ $ a is higher than that of $ a ++. Therefore, ++ $ a must first + 1. At this time, $ a = 6. $ a ++ according to the preceding Condition a ++, perform the first operation and then add one. At this time, $ a is equal to 6 (step 1) therefore, $ B = 6 + 6 = 12; Step 1 $ a = 6 for the value of third $ a; after Step 2 a ++, $ a + 1 after the calculation of $ B, $ a = 7 */

A ++ executes the expression before auto-increment. When executing the expression, it uses the value of.
++ A is the expression that is first Auto-incrementing and then executed. When executing the expression, a auto-incrementing value is used.
Therefore, the value of $ B is 5, and the value of $ a is 6.
The first question is understood, and the second question is justified.

First test, second$bYes 12

Simply put,++Add$a=$a+1
For example

$ B = ($ a ++) + 10 // equivalent to $ B = $ a + 10; $ a = $ a + 1 $ B = ++ $ a + 1 // equivalent to $ a = $ a + 1; $ B = $ a + 10;

Contain++Only one ++ can be used. It is not recommended to use multiple issues involving priority and compiler parsing.

A ++ first uses the current a value in the expression and then auto-increment.
++ A is to auto-increment a and then use the value of.

int a = 1;int b = a++;    // b == 1a = 1;int c = ++a;    // c == 2

If you really don't understand it, you just needa++Or++aUse it as a separate sentence, for example

Int a = 1; int B = a; a ++; // or ++ a; a = 1; ++ a; // or a ++; int c =;

As for the situation where a bunch of + numbers are connected together ...... If you write a program on your own, open it and write it. You will only encounter it during the exam. (If someone else sees such a program, the experiment results will be known)

A ++ adds one to the value of a after the current statement is executed.
++ A is to add the statement repeatedly executed to the value of.

After using a, add a to 1.
Add A_1 before using

Add a detail. Auto-increment is not equal to a + = 1. Unless the code is optimized to a ++ or ++. Otherwise, the commands specific to the underlying layer will be different. One is add a, 1. One is inc.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.