Compound Assignment Operators · Operadores de Atribuição Composta
| English | Português |
|---|---|
| compound assignment operator/ˈkɒmpaʊnd əˈsaɪnmənt ˈɒpəreɪtə/ | operador de atribuição composta |
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:
+=,-=,*=,/=,%=.
Atalho para atualização
- Atualizar uma variável a partir dela mesma é tão comum que tem atalho.
- Um operador de atribuição composta 复合赋值运算符 combina uma operação com
=. x += 3;significa exatamentex = x + 3;.- A família:
+=,-=,*=,/=,%=.
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.
Os cinco operadores
x += 5→ adicione5ax.x -= 5→ subtraia5.x *= 2→ dobrex.x /= 2→ divida ao meiox(regras int se aplicam).x %= 3→ substituaxporx % 3(o resto).- Cada um lê o valor atual, aplica a op e armazena o resultado de volta.
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.
Incremento e decremento
x++adiciona1ax(mesmo quex += 1oux = x + 1).x--subtrai1dex.- Estes são extremamente comuns em laços para avançar um contador.
- Eles alteram a variável no local — nenhum
=necessário.
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.
Mesmas regras de tipo se aplicam
- Operadores compostos seguem as mesmas regras de tipo da forma longa.
int x = 7; x /= 2;dá3(divisão int ainda trunca).x += 2.5;em umintexigiria cuidado — o tipo dexnão muda.- O atalho é apenas mais curto; a aritmética é idêntica.
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 é exatamente x = x + 3 — as regras de tipo não mudam. Então int x = 7; x /= 2; ainda dá 3 (divisão int trunca), não 3.5. Operadores compostos são puro atalho; se a forma longa truncasse ou tivesse overflow, o mesmo vale para a forma curta. E x++ é apenas x += 1.
Tracing a counter:
int x = 10;x += 5;→xis15.x *= 2;→xis30.x--;→xis29.x %= 5;→xis4(29 % 5).
Rastreando um contador:
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.
Operadores de atribuição composta (+=, -=, *=, /=, %=) são atalhos: x += 3 significa x = x + 3. x++ e x-- somam ou subtraem 1. Eles seguem as mesmas regras de tipo da forma longa — int divisão em x /= 2 ainda trunca.
Stepping a counter · Incrementar um contador
Each compound operator updates x in place. · Cada operador composto atualiza x no lugar.
x += 3 is exactly the same as... · x += 3 é exatamente o mesmo que...
Compound assignment is shorthand for x = x + 3. · Atribuição composta é atalho para x = x + 3.
int x = 10; x += 5; x *= 2; What is x? · int x = 10; x += 5; x *= 2; Qual é x?
10 → 15 → 30.
int x = 7; x /= 2; What is x? · int x = 7; x /= 2; Qual é x?
int division truncates: 7/2 = 3. · Divisão int truncar: 7/2 = 3.
x++ has the same effect as... · x++ tem o mesmo efeito que...
x++ adds 1, same as x += 1. · x++ adiciona 1, igual a x += 1.
Compound operators follow the same type rules as the long form (so x /= 2 on an int truncates). · Operadores compostos seguem as mesmas regras de tipo da forma longa (portanto x /= 2 em um int truncar).
Shorthand doesn't change the arithmetic — int division still truncates. · Atalho não altera a aritmética — divisão int ainda truncar.