Questions tagged [compound-assignment]

For questions about single operators that assign a value based on both a previous value and an operand (e.g., the += operator in C or Python). Also referred to as "augmented assignment." Use this tag if your problem specifically involves or relates to a compound-assignment operator.

Compound assignment operators, such as +=, combine aspects of assignment with aspects of regular binary operators. For example, i += 42 generally has the effect of i = i + 42, but is more concise.

However, that is not always the case. For example, in a Python 3 class, a custom + can be implemented using the __add__ method, and a custom += can be implemented using the __iadd__ method. Those two methods could behave very differently.

102 questions
174
votes
17 answers

What exactly does += do?

I need to know what += does in Python. It's that simple. I also would appreciate links to definitions of other shorthand tools in Python.
Salvatore Mucciolo
  • 1,785
  • 2
  • 11
  • 4
117
votes
7 answers

Operator precedence with JavaScript's ternary operator

I can’t seem to wrap my head around the first part of this code ( += ) in combination with the ternary operator. h.className += h.className ? ' error' : 'error' The way I think this code works is as follows: h.className = h.className + h.className…
95
votes
8 answers

Shortcut "or-assignment" (|=) operator in Java

I have a long set of comparisons to do in Java, and I'd like to know if one or more of them come out as true. The string of comparisons was long and difficult to read, so I broke it up for readability, and automatically went to use a shortcut…
David Mason
  • 2,917
  • 4
  • 30
  • 45
93
votes
12 answers

Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

So for binary operators on booleans, Java has &, |, ^, && and ||. Let's summarize what they do briefly here: JLS 15.22.2 Boolean Logical Operators &, ^, and | JLS 15.23 Conditional-And Operator && JLS 15.24 Conditional-Or Operator || For &, the…
89
votes
7 answers

Is there an explanation for inline operators in "k += c += k += c;"?

What is the explanation for the result from the following operation? k += c += k += c; I was trying to understand the output result from the following code: int k = 10; int c = 30; k += c += k += c; //k=80 instead of 110 //c=70 and currently I am…
77
votes
3 answers

Why are there no ||= or &&= operators in C#?

We have equivalent assignment operators for all Logical operators, Shift operators, Additive operators and all Multiplicative operators. Why did the logical operators get left out? Is there a good technical reason why it is hard?
74
votes
6 answers

Do the &= and |= operators for bool short-circuit?

When writing code like this in C++: bool allTrue = true; allTrue = allTrue && check_foo(); allTrue = allTrue && check_bar(); check_bar() will not be evaluated if check_foo() returned false. This is called short-circuiting or short-circuit…
66
votes
3 answers

Lua operators, why isn't +=, -= and so on defined?

This is a question I've been mildly irritated about for some time and just never got around to search the answer to. However I thought I might at least ask the question and perhaps someone can explain. Basically many languages I've worked in utilize…
qrikko
  • 2,483
  • 2
  • 22
  • 35
59
votes
3 answers

Addition assignment += behavior in expression

Recently I came across this question: Assignment operator chain understanding. While answering this question I started doubting my own understanding of the behavior of the addition assignment operator += or any other operator= (&=, *=, /=, etc.). My…
11thdimension
  • 10,333
  • 4
  • 33
  • 71
31
votes
1 answer

Varying behavior for possible loss of precision

In Java, when you do int b = 0; b = b + 1.0; You get a possible loss of precision error. But why is it that if you do int b = 0; b += 1.0; There isn't any error?
szupie
  • 826
  • 10
  • 18
28
votes
5 answers

Java boolean |= operator

Recently I saw a code using this: boolean val = something(); val |= somethingElse(); Interesting part is |= (binary like) operator made on boolean primitive type. It surprised me that |= exist for boolean, as if it was integer type, and searched…
27
votes
4 answers

Difference between string += s1 and string = string + s1

One of my programs is exceeding the time limit when I am using fans = fans + s[i], while when I am using fans += s[i] it is being accepted... Why does this happen? To Explain more , fans is a string and s is also a string so while iterating over…
Naman
  • 353
  • 3
  • 14
24
votes
1 answer

What does compound let/const assignment mean?

There is an article Optimization killers in wiki of Bluebird library. In this article there is a phrase: Currently not optimizable: ... Functions that contain a compound let assignment Functions that contain a compound const…
Alexander Myshov
  • 2,881
  • 2
  • 20
  • 31
20
votes
6 answers

Why is "a^=b^=a^=b;" different from "a^=b; b^=a; a^=b;"?

I tried some code to swap two integers in Java without using a 3rd variable, using XOR. Here are the two swap functions I tried: package lang.numeric; public class SwapVarsDemo { public static void main(String[] args) { int a = 2984; …
Abhishek Oza
  • 3,340
  • 1
  • 27
  • 35
16
votes
2 answers

Is *p++ += 2 well defined?

I'm not sure whether statement below is well defined by standard C or not *p1++ += 2; or other similar statement: *E1++ = E2 From standard C about post-increment: The result of the postfix ++ operator is the value of the operand. …
dragon135
  • 1,366
  • 8
  • 19
1
2 3 4 5 6 7