according to my textbook, since i = 1, then ++i should have 2 stored in i, and i++ should have 1 stored in i. This is the case when I output i individually, but how come when I try to output both i++ and ++i / vice-versa in one statement, the output seems to be completely off?
Your putting the entire command on one line which is causing your confusion.
Try
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()
{
int i = 1;
cout << ++i << endl;
i = 1;
cout << i++ << endl;
// not the same as
i = 1;
cout << ++i << endl;
// i = 1;
cout << i++ << endl;
return 0 ;
}
oh thank, i've tried to write the codes in separate lines and it's outputting correctly. But i'm just curious, why when I write everything on a single line, eg)
cout << ++i << " " << i++; the output is: 3 1 ?