Compound Assignment Operators · 复合赋值运算符
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| compound assignment operator/ˈkɒmpaʊnd əˈsaɪnmənt ˈɒpəreɪtə/ | 复合赋值运算符 | fù hé fù zhí yùn suàn fú |
Shorthand for updating
- Updating a variable from itself is so common it has shorthand.
- A compound assignment operator 复合赋值运算符 combines an operation with
=. x += 3;means exactlyx = x + 3;.- The family:
+=,-=,*=,/=,%=.
更新的简写
- 用变量自己更新自己太常见了,于是有了简写。
- 复合赋值运算符把一个运算与
=结合起来。 x += 3;恰好表示x = x + 3;。- 这一家族:
+=、-=、*=、/=、%=。
The five operators
x += 5→ add5tox.x -= 5→ subtract5.x *= 2→ doublex.x /= 2→ halvex(int rules apply).x %= 3→ replacexwithx % 3(the remainder).- Each reads the current value, applies the op, and stores the result back.
五个运算符
x += 5→ 给x加5。x -= 5→ 减5。x *= 2→ 让x翻倍。x /= 2→ 让x减半(适用 int 规则)。x %= 3→ 把x换成x % 3(余数)。- 每个都读取当前值,应用运算,再把结果存回去。
Increment and decrement
x++adds1tox(same asx += 1orx = x + 1).x--subtracts1fromx.- These are extremely common in loops to step a counter.
- They change the variable in place — no
=needed.
自增与自减
x++给x加1(等同于x += 1或x = x + 1)。x--给x减1。- 这些在循环里步进计数器时极其常见。
- 它们就地改变变量——不需要
=。
Same type rules apply
- Compound operators obey the same type rules as the long form.
int x = 7; x /= 2;gives3(int division still truncates).x += 2.5;on anintwould need care — the type ofxdoesn't change.- The shorthand is only shorter; the arithmetic is identical.
同样适用类型规则
- 复合运算符遵守与长写法相同的类型规则。
int x = 7; x /= 2;得到3(int 除法仍然截断)。- 对一个
int用x += 2.5;需要小心——x的类型不会改变。 - 简写只是更短;算术是完全一样的。
x += 3 is exactly x = x + 3 — the type rules don't change. So int x = 7; x /= 2; still gives 3 (int division truncates), not 3.5. Compound operators are pure shorthand; if the long form would truncate or overflow, so does the short form. And x++ is just x += 1.
**x += 3 恰好是 x = x + 3——类型规则不变。**所以 int x = 7; x /= 2; 仍得到 3(int 除法截断),而非 3.5。复合运算符纯属简写;如果长写法会截断或溢出,短写法也会。而 x++ 就是 x += 1。
Tracing a counter:
int x = 10;x += 5;→xis15.x *= 2;→xis30.x--;→xis29.x %= 5;→xis4(29 % 5).
追踪一个计数器:
int x = 10;x += 5;→x是15。x *= 2;→x是30。x--;→x是29。x %= 5;→x是4(29 % 5)。
Compound assignment operators (+=, -=, *=, /=, %=) are shorthand: x += 3 means x = x + 3. x++ and x-- add or subtract 1. They obey the same type rules as the long form — int division in x /= 2 still truncates.
复合赋值运算符(+=、-=、*=、/=、%=)是简写:x += 3 表示 x = x + 3。x++ 和 x-- 加或减 1。它们遵守与长写法相同的类型规则——x /= 2 中的 int 除法仍然截断。
Stepping a counter · 步进一个计数器
Each compound operator updates x in place. · 每个复合运算符就地更新 x。
x += 3 is exactly the same as... · x += 3 恰好等同于……
Compound assignment is shorthand for x = x + 3. · 复合赋值是 x = x + 3 的简写。
int x = 10; x += 5; x *= 2; What is x? · int x = 10; x += 5; x *= 2; x 是多少?
10 → 15 → 30. · 10 → 15 → 30。
int x = 7; x /= 2; What is x? · int x = 7; x /= 2; x 是多少?
int division truncates: 7/2 = 3. · int 除法截断:7/2 = 3。
x++ has the same effect as... · x++ 的效果等同于……
x++ adds 1, same as x += 1. · x++ 加 1,等同于 x += 1。
Compound operators follow the same type rules as the long form (so x /= 2 on an int truncates). · 复合运算符遵守与长写法相同的类型规则(所以对 int 的 x /= 2 会截断)。
Shorthand doesn't change the arithmetic — int division still truncates. · 简写不改变算术——int 除法仍然截断。