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
2
votes
1 answer

Why is this assignment ambiguous?

Please note that this question is not about how to change the code below to make it work; rather, I am looking for some insight on why a compiler would find this assignment ambiguous: entity assignment_to_aggregates is end; architecture example of…
VHDL Addict
  • 171
  • 1
  • 10
2
votes
3 answers

python / sets / dictionary / initialization

Can someone explain help me understand how the this bit of code works? Particularly how the myHeap assignment works. I know the freq variable is assigned as a dictionary. But what about my myHeap? is it a Set? exe_Data = { 'e' :…
delgeezee
  • 103
  • 1
  • 7
1
vote
1 answer

Different results for if-else expression in R

I am getting different results for changings order of "d + 20" and "print("d is smaller than 100")" within if-else in R. Please see the code below and help explain the difference. As far as I know, value of a compound expression is the value of the…
1
vote
5 answers

What does *= do?

Hey I am kinda new to C and I wanted to ask why this prints out 4 instead of 260? #include int main() { unsigned char x = 130; x *= 2; printf("%d\n", x); }
Alpha
  • 319
  • 2
  • 12
1
vote
1 answer

Getting wrong result when using Compound multiplication statement - C#

I noticed something very strange. In the code snippet below, the result outputted on the Console is always 0 int result = 0; for(int i = 1; i < 4; i++) { result *= 10 + i; } Console.WriteLine(result); It looks like result *= 10 + i; always…
Vikram Singh
  • 435
  • 2
  • 10
1
vote
0 answers

Does bitwise inclusive OR with assignment operator have advantages over typical assignment in java?

When looking at some code on one or another git, sometimes I can see that devs use bitwise inclusive OR compound assignment operator (|=) where simple assignment would be enough. Unfortunately, I don't have any code with this solution at hand, so…
Andret2344
  • 653
  • 1
  • 12
  • 33
1
vote
2 answers

How to use python assignment operator with multiple return values

I have a Python function which takes a parameter and returns 2 outputs. I want to call this for all the elements in a list and combine the return values for each invocation as below: a1, b1 = func(p[0]) a2, b2 = func(p[1]) a3, b3 = func(p[3]) a =…
locke14
  • 1,335
  • 3
  • 15
  • 36
1
vote
1 answer

Overloading composed operators (+=,-=,*= and /=). Doesn't calculate how it should

The code is a part of a more complex one, but i reduced to see the part where i have the problem. So after overloading the +=, -=,*= and /= operators,the results are all the same and i don't know what i'm doing wrong. The header file looks like…
1
vote
1 answer

Exception thrown: read access violation. this was nullptr in array of objects

I'm a CS student taking an OOP course and I don't know how to fix this issue. I understand that when the += operator tries to add the first element into the array, 'this' is nullptr and an exception is thrown, but I don't know how to fix…
1
vote
2 answers

Check CompundDrawable of TextView with Espresso

I am new to Espresso tests and Android. I am trying to test if the correct Icon is shown next to the text. The icon is set with: public void setLabelTextIcon(@DrawableRes int iconResId) { …
1
vote
2 answers

Overloading compound-assignment-operator in C++ does not change variable

In order to become more familiar with C++, I'm implementing a class to manipulate complex numbers. class Complex { private: double _real; double _imag; public: Complex(); Complex(double real, double imag); …
Gilfoyle
  • 3,282
  • 3
  • 47
  • 83
1
vote
5 answers

compounding / while loops

#include int main(void) { int days, hours, mins; float a, b, c, total, temp, tempA, tempB; a = 3.56; b = 12.50; c = 9.23; total = a+b+c; days = total / 24; temp = total/24 - days; hours = temp * 24; tempA = temp*24…
Thao Nguyen
  • 901
  • 7
  • 22
  • 42
1
vote
1 answer

Overloading "+=" operator: c+=a_times_b works, but c+=a*b does not?

I have two classes A and B. The product A*B should be of type B. Since variables of type B will occupy great amounts of memory, I need to avoid operations like B_1 = B_1 + (A_1 * B_2), where (A_1 * B_2) would be temporarily allocated. In other…
1
vote
2 answers

Compound assignment operators with self in C++

To compute the square of 2.0, does this code double a = 2.0; a *= a; have well defined behavior? And, equivalently, with all the other compound assignment operations and build-in types.
levzettelin
  • 2,600
  • 19
  • 32
1
vote
1 answer

Shifting a type char, similar assignments behave differently

Hi I have a piece of Java code that shifts a character by 2, like so char ch = 'A'; ch += 2; System.out.println(ch); The output in this case is 'C' (as expected). But if I rewrite the code like this: char ch = 'A'; ch = ch +…
Robb Hoff
  • 1,719
  • 2
  • 17
  • 32