Questions tagged [compound-operator]

In C-like languages,"x = x + y" can be coded as "x += y". This is a compound operator. There is such an operator for every arithmetic, logical and bit-wise operator.

x = x + y can be abbreviated to x += y.

These are similar in effect, but not identical for all languages.

For example, in Java, x += y is equivalent to x = (type-of-x)(x + y), for example:

char x = 'a';
x += 1; // 'b'

compare with:

char x = 'a';
x = x + 1; // compile error: "incompatible types: possible lossy conversion from int to char"
12 questions
8
votes
3 answers

Why would i+=l compile, where i is int and l is long?

I spotted Java's +=, -=, *=, /= compound assignment operators (good question :)), but it had a part that I don't quite understand. Borrowing from that question: int i = 5; long l = 8; Then i = i + l; will not compile but i += l; will compile…
user
  • 6,897
  • 8
  • 43
  • 79
8
votes
1 answer

What is the difference of x=x+3 and x+=3? Why one needs type cast and the other does not?

Question : char x = 'a'; x += 3; // ok x = x + 3; // compile time error
user845932
  • 531
  • 1
  • 6
  • 4
8
votes
3 answers

Java: += equivalence

Is: x -= y; equivalent to: x = x - y;
dukevin
  • 22,384
  • 36
  • 82
  • 111
5
votes
3 answers

Is there a compound assignment operator for a = b a (where is not commutative)?

In a lot of languages a = a + b can be written as a += b In case of numerical operations, a + b is same as b + a, so the single compound operator suffices. Also, a = a - b can be written as a -=b . However, a-b is not equal to b-a. Hence, the…
asheeshr
  • 4,088
  • 6
  • 31
  • 50
4
votes
3 answers

java bitwise operators and the equal character; compound operators

I'm a bit confused: long v = 0; v <<= 8; v |= 230; I know << is the signed left shift operator and | Bitwise the inclusive OR but I'm confused to what the equals does? So fist v is 0. So << doesn't have any effect? Then it equals 1000 but what…
Thomas
  • 1,678
  • 4
  • 24
  • 47
2
votes
1 answer

How to preserve type guard narrowing when using logical assignment?

The following is an example that reproduces the problem: type F = () => number; type R = { [ x: string ]: R | F | undefined } const isFunc = any>(maybe:unknown) : maybe is T => typeof maybe === "function"; const…
2
votes
3 answers

using print() with compound operators in python

The following code doesn't work in python x = 11 print(x += 5) while this code does x = 11 x += 5 print(x) why is that?
Mahmoud Hanafy
  • 7,958
  • 12
  • 47
  • 62
2
votes
1 answer

C4244 level 4 warning on compound addition assignment, but not on sum and assignment

In MSVS 2005, The following C code generates one warning: int main() { short a = 0; a += a; // C4244 (level 4) a = a + a; // OK } The warning message is: warning C4244: '+=' : conversion from 'int' to 'short', possible loss of data The…
Ingo Schalk-Schupp
  • 843
  • 10
  • 26
2
votes
5 answers

Automatically catching the use of =+ rather than += in Java

I have this horrible habit of typing the below and not catching it until well into testing: int i = 1; int j = 2; i =+ j; //i equals 2, not 3 as intended; assign only, + is unary and works on the j The correct version, of course, would be int i =…
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
0
votes
2 answers

How to use the compound operators of MySQL 8.0 in the simplest syntax?

https://www.w3schools.com/mysql/mysql_operators.asp Came across this. I am studying MySQL as a beginner and I can't seem to find anything on the internet that works. I can't even find them in…
askein
  • 39
  • 4
0
votes
5 answers

C - Logical Compound Operators

Is the compound operator '&=' logical or bitwise AND ? In other words, is a &= b the same as: a = a & b a = a && b
Zyyk Savvins
  • 483
  • 4
  • 8
  • 14
-1
votes
2 answers

Java Compound Operator

I have a loop for palindrome checking and I want to ask why does it work when i do b=b*10+a but when I do b*=10+a it doesn't work even though it's short for b=b*10+a. int a=0,b=0; while (number>0) { a=number%10; b=b*10+a; …