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
4
votes
6 answers

Advantage of using compound assignment

What is the real advantage of using compound assignment in C/C++ (or may be applicable to many other programming languages as well)? #include int main() { int exp1=20; int b=10; // exp1=exp1+b; exp1+=b; return 0; }; I…
Rajesh
  • 1,085
  • 1
  • 12
  • 25
4
votes
2 answers

Why implicit conversion occurs for addition between unsigned char?

GCC warns this code: unsigned char i = 1; unsigned char j = 2; i += j; says: warning: conversion to 'unsigned char' from 'int' may alter its value [-Wconversion] i += j; ^ It seems that j is implicitly converted to int. Why the implicit…
equal-l2
  • 899
  • 7
  • 17
4
votes
4 answers

Is there a ruby oneliner to concat nested arrays without temporary copies?

a = [ 'a' ] b = [ 'b' ] def c return [ 'c' ], [ 'd' ] end a, b += c # -> would be awesome, but gives syntax error a, b = a + c.first, b + c.last # clunky and will call method twice... # desired result # a == [ 'a', 'c' ] b == [ 'b', 'd'…
user1115652
4
votes
3 answers

Is there a cleaner way to add "else if" to assignment conditional in Awk, etc.?

Certain languages like awk script allow for conditional assignments. For example, say you had a list file in the format: e.g. Grape 4.99 JuicyFruitGum 0.45 Candles 5.99 And you wanted to tax everything over…
4
votes
2 answers

Are compound statements lvalue (or rvalue) in C?

When I examined the definition of the container_of macro in Linux Kernel, I saw a compound statement as a macro definition, #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); …
albin
  • 773
  • 1
  • 8
  • 27
4
votes
1 answer

Chaining compound assignment operators in JavaScript

In C#, string s = "abc"; s += (s += s); Console.WriteLine(s); writes abcabcabc (http://ideone.com/pFNFX2). This is fine, because the C# specification explicitly says in section 7.16.2 that the operation is evaluated as x = x op y, except that x…
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
3
votes
2 answers

vectorize the assignment of 3d numpy arrays conditioned on the associate values at other dimensions

Is it possible to vectorize the following code in Python? It runs very slowly when the size of the array becomes large. import numpy as np # A, B, C are 3d arrays with shape (K, N, N). # Entries in A, B, and C are in [0, 1]. # In the following, I…
Jayyu
  • 319
  • 2
  • 10
3
votes
1 answer

Compoud operator (+=) and && in C, weird value

Looking at this code: #include #include int main(){ int a = -1; int b = 0xfc; // = 252 b+=a && a++; printf("%d %d\n", a, b); } the output I think should be is: 0 251 Actually, the real output is: 0 253 But why?…
Loris Simonetti
  • 217
  • 2
  • 10
3
votes
2 answers

Compound assignment in C++

I would like to know the execution flow of compound assignments in C++. I came across a CodeChef question, where I am calculating NCR mod p values and adding them together to get the final answer: // correct for(int i=min1; i<=max1; i+=2){ ans…
Rahul
  • 1,858
  • 1
  • 12
  • 33
3
votes
1 answer

C: Error in Using: "Compound Assignment" and "Prefix Decrement" together

Can someone please tell me why the C compiler outputs an error while using a Compound Assignment and a Prefix Dec/Inc together ? [ but C++ does not ] int myVar = 5; (--myVar) -= 4; // C : error C2106: '-=' : left operand must be l-value // C++:…
Emadpres
  • 3,466
  • 2
  • 29
  • 44
3
votes
2 answers

Java Compound Assignment Operator precedence in expressions

The output of the following code is declared as "6" when I try to execute this. When I am trying to think through this, the expression "k += 3 + ++k; should have been evaluated as k = k + (3 + ++k); but in this case the output should have been 7.…
sunsin1985
  • 2,437
  • 5
  • 22
  • 27
2
votes
3 answers

Why does _ret evaluate to true, shouldn't it evaluate to false (Bit Operator)?

class Program { private static bool _ret = true; static void Main() { _ret &= Method(); Console.WriteLine(_ret); Console.Read(); } private static bool Method() { _ret &= false; return…
erothvt
  • 73
  • 5
2
votes
3 answers

c: What does this line do?

I read some code and came over this rather cryptic syntax: size_t count = 1; char *s = "hello you"; char *last_word = "there"; count += last_word < (s + strlen(s) - 1); #line of interest Count is incremented, somehow. But I thought the <…
2
votes
1 answer

Sequence point within assignment operators

Let's just take for example the specific compound assignment operator ^=. This stackoverflow page says modification of the left operand may have not been done after the evaluation of ^=, and thus making the code a ^= b ^= a ^= b undefined behaivor.…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
2
votes
1 answer

Overload compound operators like +=

Is it possible to overload operator += directly in Scala? It might be useful for some complex types, where a += b may have more efficient and simpler implementation that a = a + b. The case I encountered recently is I am providing operators for…
Suma
  • 33,181
  • 16
  • 123
  • 191