1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; int main(){ int n1,n2=5;//赋值令n1和n2的值都是5 n2++;//n2增加1, cout<<"n2="<<n2<<endl; n2--; cout<<"n2="<<n2<<endl; ++n2;//n2增加1 cout<<"n2="<<n2<<endl; n1=n2++;//++在变量右侧,则先取变量值再自增,即先将n2的值7赋给n1,再将n2增加1变成8 cout<<"n1="<<n1<<",n2="<<n2<<endl; n1=++n2;//++在变量左侧,则先自增再取值,即先将n2增加1变成9,再将n2的值9赋给n1 cout<<"n1="<<n1<<",n2="<<n2<<endl; return 0; } |
总结:⾃增(或⾃减)运算符在计算时,如果是后缀式⾃增(或⾃减),即 i++形式的,那么会在结束当前语句的执⾏后,进⾏⾃增(或⾃减)运算。