上代码:
#include<iostream> int main() { using namespace std; int i=1; cout << "开始i = " << i << endl; cout << "i++ = " << i++ << endl; cout << "i++后,i = " << i << endl; cout << "++i = " << ++i << endl; cout << "++i后,i = " << i << endl; cout << "i-- = " << i-- << endl; cout << "i--后,i = " << i << endl; cout << "--i = " << --i << endl; cout << "--i后,i = " << i << endl; system("pause"); return 0; }
输出:
开始i = 1 i++ = 1 i++后,i = 2 ++i = 3 ++i后,i = 3 i-- = 3 i--后,i = 2 --i = 1 --i后,i = 1 请按任意键继续. . .
结论:
①i++: i++显示的是i的值,在显示之后,i=i+1;
②++i: ++i显示的是i+1的值,显示之后,i=i(这个时候i是+1后的i);
③i--: i--显示的是i的值,在显示之后,i=i-1;
④--i: --i显示的是i-1的值,在显示之后,i=i(这个时候i是-1后的i)。
即:
i在加减符号前的,先显示i再进行加减;
i在加减符号后的,先加减,再显示i。