1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include <iostream>// 输出流库 using namespace std;//使用标准命名空间 //课程:3.3 运算符 递增递减 int main() { //前置递增 int a = 10; ++a;//让变量+1 cout << "前置递增 a =" << a << endl; //后置递增 int b = 10; b++;//让变量+1 cout << "后置递增 b =" << b << endl; //前置递增与后置递增的区别 //前置递增 先让变量+1 再进表达式运算 int a1 = 10; int b1 = ++a1 * 10;//(a1+1)*b1=110 cout << "a1 = " << a1 << endl; cout << "b1 = " << b1 << endl; //后置递增 先表达运算后+1 int a2 = 10; int b2 = a2++ * 10;//a2+1 (a2*b2) cout << "a2 = " << a2 << endl; cout << "b2 = " << b2 << endl; //前置递减 int s = 10; --s;//让变量-1 cout << "前置递减 s =" << s << endl; //后置递减 int d = 10; d--;//让变量-1 cout << "后置递减 d =" << d << endl; //前置递减与后置递增的区别 //前置递减 先让变量-1 再进表达式运算 int s1 = 10; int d1 = --s1 * 10;//(s1-1)*d1=90 cout << "s1 = " << s1 << endl; cout << "d1 = " << d1 << endl; //后置递增 先表达运算后-1 int s2 = 10; int d2 = s2-- * 10;//s2-1 (s2*d2) cout << "s2 = " << s2 << endl; cout << "d2 = " << d2 << endl; system("pause");// 控制台暂停,等待下一步操作 return 0;// 结束返回值:0 } |
03.3 C++运算符递增递减
未经允许不得转载:Ai分享 » 03.3 C++运算符递增递减