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
0
votes
2 answers

How can you use compound assignment operators in C if you can't redefine variables?

Looking at wikipedia it says: a -= b; is the same as a = a - b; But when I try this in my C program I get the following error: "error: redefinition of 'a'". Here is my program: #include int main(int argc, char *argv[]) { int a =…
CClarke
  • 503
  • 7
  • 18
0
votes
1 answer

Concern with the vocabulary of JLS

I always see this strange symbol § but never understand wtf means Also what E1, T, op means here? From the post: Why don't Java's +=, -=, *=, /= compound assignment operators require casting? Correct me: T = data type op = operator
Pablo_06
  • 35
  • 5
0
votes
4 answers

Confusion about compound assingnments (+=, -=, *=, ...) in Java

I'm a little confused about the result of the following code: int x = 1; x -= ((x += 1) << 1); System.out.println(x); It prints out -3, but I'd expected it to print out -2, because in my head the computation should go like this: |…
0
votes
2 answers

+= in Python when assigning it to third variable

When I was looking at meaning of the += operator in Python, I looked at the answers to a similar question: What exactly does += do in python?. But in the below code excerpt: increments += arr[i-1] - arr[i] There is a third variable used. If I…
Sreedhar Danturthi
  • 7,119
  • 19
  • 68
  • 111
0
votes
1 answer

c compound assignment increment only right bits

I am new to C and working with a serial device and I have this line: short DA, DacData; DA=0xFFF; DacData = ((channel&0x03)<<14)|((serialA&0x03)<<12)|(DA&0x6AA); How do I increment only the last part of DacData (DA&0x6AA) in my loop? Thanks in…
0
votes
2 answers

Better way to write 'assign A or if not possible - B'

So, in my code I have a dictionary I use to count up items I have no prior knowledge of: if a_thing not in my_dict: my_dict[a_thing] = 0 else: my_dict[a_thing] += 1 Obviously, I can't increment an entry of a value that doesn't exist yet.…
Sanuuu
  • 243
  • 1
  • 10
0
votes
4 answers

Optimizing repeating assignment of variable values

In an effort to learn compound assignment in C++, I created the following code to demonstrate what they do: int b05; int b06 = 13; b05 = 49; b05 += b06; // b05 = b05 + b06 cout << "(+=) compound assignment: " << b05 << endl; b05 = 49; b05 -= b06;…
0
votes
1 answer

How does the or-assignment operator (|=) work? (C#)

I've seen documentation and answers (1) (2) that try to explain what the |= operator is and how it works, and while it kind of makes sense on a basic level... I don't quite get why or how it accomplishes what it does. The explanations say that a |=…
0
votes
1 answer

c++ presedence of operators

Hi guys so I need some help understanding how these compound assignment operators work for example int x=6; x += x -= x * x; x turns out to be -60 can someone explain why and how this works?
0
votes
1 answer

Isn't parameter type of function like "string& operator+= (const string& str)" confusing? (reference to const)

string& operator+= (const string& str); When we look at the function prototype, we assume that the string passed into the function won't be changed, right? However, consider the situation: string str("Example"); str += str; The string 'str' was…
0
votes
0 answers

JavaScript run calculation after radio button checked

New to JavaScript and can't get my head around this exercise: Need to modify JavaScript so it provides for the option buttons. When the user selects yearly interest rate button, the application should compound the yearly rate and when the user…
0
votes
2 answers

How to hold a const value in a struct in C (not C++)?

How can I hold a constant value in a struct? If I put const at LEBEL0 I would not be able to assign to it at LEBEL1. But if I do not put const at LEBEL0, then I will get qualifier lost warning at LEBEL1. Is there any way to do this? I came up with a…
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135
0
votes
1 answer

BlueJ Java compound condition

im currently doing some code and am having problem and cant get my head around it. I am very new at this is any help would be appreciated I'm trying to make a compound condition for the date but cant seem to understand how to do it. public void…
0
votes
3 answers

Using the result of compound assignment as an lvalue

I'm surprised that this works: double x = 3; double y = 2; (x *= 2) += y; std::cout << x << std::endl; The result is 8, which is what it looks like the programmer is trying to achieve. But I thought assignment operators returned an rvalue - how…
Tom
  • 7,269
  • 1
  • 42
  • 69
0
votes
2 answers

What is the meaning and name for "+=" in C++?

I am fairly new to C++ and I have been reading and writing some of my own code. I see these operators from time to time, if that is even the right word to use? += // Not sure what it means So my question is: what do they mean/do, and what are they…
dusz
  • 913
  • 1
  • 9
  • 15