| English | 의미 |
|---|---|
| compound assignment operator/ˈkɒmpaʊnd əˈsaɪnmənt ˈɒpəreɪtə/ | 합성 대입 연산자 |
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:
+=,-=,*=,/=,%=.
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.
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.
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.
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.
Tracing a counter:
int x = 10;x += 5;→xis15.x *= 2;→xis30.x--;→xis29.x %= 5;→xis4(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를原位으로 업데이트합니다.
x += 3은 다음과 정확히 동일합니다...
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
합성 대입은 x = x + 3의 줄임말입니다.
int x = 10; x += 5; x *= 2; x의 값은?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
10 → 15 → 30.
int x = 7; x /= 2; x의 값은?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
int 나눗셈에서는 소수점 이하를 버림: 7/2 = 3.
x++는 다음과 동일한 효과를 가집니다...
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
x++는 1을 더하는 것이므로 x += 1과 같습니다.
합성 연산자는 원본 형식의 같은 타입 규칙을 따르므로(x /= 2가 int에서 적용되면 소수점 이하를 버림).
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
줄임표기법으로 산술 연산이 바뀌지는 않습니다 — int 나눗셈은 여전히 소수점 이하를 버립니다.