01-基础语法/04-赋值与数学运算:修订间差异
跳转到导航
跳转到搜索
小 导入1个版本 |
->Importer 批量导入三三文档 |
||
(没有差异)
| |||
2026年5月20日 (三) 16:50的版本
赋值语句
变量 = 表达式;
计算右边表达式的值,然后把值存入左边变量中。
整个赋值语句的值为赋值后的值,因此可以写 a = b = c = 0; 这样的连续赋值。
语法糖
- a += b; [math]\displaystyle{ \to }[/math] a = a + b;
- a -= b; [math]\displaystyle{ \to }[/math] a = a - b;
- a *= b; [math]\displaystyle{ \to }[/math] a = a * b;
- a /= b; [math]\displaystyle{ \to }[/math] a = a / b;
- a %= b; [math]\displaystyle{ \to }[/math] a = a % b;
- a++; — 将 a 增加 [math]\displaystyle{ 1 }[/math];作为表达式时,值为增加之前的值
- ++a; — 将 a 增加 [math]\displaystyle{ 1 }[/math];作为表达式时,值为增加之后的值
数学运算符
- +、-、*、/、%:加法、减法、乘法、除法、取余/取模
- ():数学中的小括号、中括号、大括号在 C++ 中统统用小括号
- 如果两个运算数都是整数,运算结果也会是整数(除法变成整除)
- 只要有一个运算数是小数(浮点数),运算结果就会得到小数
- 取余只能在整数间进行!不能除以 [math]\displaystyle{ 0 }[/math] 或对 [math]\displaystyle{ 0 }[/math] 取余!