Home >Backend Development >PHP Problem >The difference between a++ and ++a
The difference between a and a
1. The calculation results are different. The former result is related to the program execution process. It may be added 1. It is possible not to add 1, but the result of the latter is a plus 1;
2. The calculation process is different. The former calculation process is to execute a first and then add 1, while the latter calculation process is to add 1 first. Go to 1, and then execute a.
Example question
int a = 100; a is 100 at the beginning.
Print a; This printf will print the value of a. Note that it is the value of the expression a. According to my previous explanation, the value of a is the value of a, which is 100. Don't forget that the value of a will increase by 1 after this statement ends (that is, a side effect has occurred).
Print a; The current value of a is 101. Now we want to print the value of the a expression. The value of a is equal to a 1, so 102 will be printed. After this statement a=101 1=102.
Print --a; This time we want to print the value of the --a expression, which is the same as self-increment. The value of --a is equal to a-1, so 101 will be printed. After this statement a=102-1=101.
Print a--; a--The value of the expression is the value of a, so 101 is printed. After this statement a=101-1=100.
Print a 1; This expression has nothing to do with auto-increment, it just prints the value of a 1, which is 101. There is no effect on the value of a itself (additive expressions have no side effects).
Print a; At this time, of course the value of a is still 100.
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of The difference between a++ and ++a. For more information, please follow other related articles on the PHP Chinese website!