explination
int n=12;
cout<< n++<<endl;//12
cout<< ++n<<endl;//14
cout<< n+++1<<endl;//15
cout<< ++n+1<<endl;//17
cout<< n+1<<endl;//17
cout<< n<<endl;//16
i have a questin since n 12 n++ must be 13? why is this value not displayed instead it is 12 in the first cout?
The post-fix increment/decrement operator returns a copy of its operand before increment/decrementing it. This behaviour is equivalent to this:
1 2 3
|
int n(12);
std::cout << n;
n += 1;
|
The pre-fix increment/decrement operators increments/decrements the value of its operand, then returns the new value.
Wazzak
Last edited on
can you please explain till the first 17? how do we get there...this is getting more clear for me but still i need to know the logic.
n++ n increments after instruction was made
++n n increments before instruction
using n++ && ++n
1 2 3 4 5 6 7
|
int n=12;
cout<< n++<<endl;//12
cout<< ++n<<endl;//14
cout<< n+++1<<endl;//15
cout<< ++n+1<<endl;//17
cout<< n+1<<endl;//17
cout<< n<<endl;//16
|
without n++ && ++n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int n=12;
cout<< n<<endl;//12
n+=1; //n=13
n+=1; //n=14
cout<< n<<endl;//14
cout<< n +1<<endl;//15
n+=1 ; //n=15
n+=1 ; //n=16
cout<< n +1<<endl;//17
cout<< n+1<<endl;//17
cout<< n<<endl;//16
|
[/code]
Last edited on
Topic archived. No new replies allowed.