I ++ adds 1 after use
++ I add 1 before use
Int A = ++ I;
How is it calculated?
++ I;
++ I;
A = I + I = 2 + 2 = 4;
How is I ++ calculated?
A = I ++;
First execute a = I + I;
Then I ++;
I ++;
Let's look at the following code:
// Example 1 # include <iostream> # include <cstdlib> using namespace STD; void main () {int C = 0, a = 0, B = 0; cout <"twice using ++ a" <Endl; C = (++ A) + (++ ); cout <"the value of C is" <C <Endl; C = a +; cout <"the value of C is" <C <Endl; cout <"twice using B ++" <Endl; C = (B ++) + (B ++); cout <"the value of C is" <C <Endl; C = B + B; cout <"the value of C is" <C <Endl; a = 0; // re-initialize the combination of variable A cout <"A ++ and ++ a, and a ++ is in front of" <Endl; C = (++) + (A ++); cout <"the value of C is" <C <Endl; C = a +; cout <"the value of C is" <C <Endl; a = 0; // re-initialize the combination of variable A cout <"A ++ and ++ a, and a ++ is later" <Endl; C = (a ++) + (++ A); cout <"the value of C is" <C <Endl; C = a +; cout <"the value of C is" <C <Endl; a = 0; // re-initialize the variable A cout <"A ++ and two ++ a mixed use" <Endl; C = (a ++) + (++ A); cout <"the value of C is" <C <Endl; C = a +; cout <"the value of C is" <C <Endl; a = 0; // re-initialize the variable A cout <"mixed use of two A ++ and one ++ a" <Endl; C = (a ++) + (A ++) + (++ A); cout <"the value of C is" <C <Endl; C = a +; cout <"the value of C is" <C <Endl; System ("pause ");}
The execution result is as follows: