10 AnswersNew Answer
For a binary operator op, a compound assignment expression of the form x op= y is equivalent to x = x op y except that x is only evaluated once. The following example demonstrates the usage of compound assignment with arithmetic operators: int a = 5; a += 9; // a = 5+9 output = 14
Suppose you had an int variable, sum, and you wanted to add 10 to it. How would you do this? Here's one solution : sum = sum + 10; This kind of operation is very common (where 10 is replaced by whatever value you want). //OR You can use the compound addition assignment operator, which looks like: += (plus sign, followed by equal sign). sum += 10; // Add 10 to sum Suppose, instead of adding a value to sum, you wanted to double it? That is, you wanted to multiply it by 2? One way to do this is : sum = sum * 2; // Multiply sum by 2 //OR sum *= 2; // Multiply sum by 2
+ is an arithmetic operator while += is an assignment operator.. When += is used, the value on the RHS will be added to the variable on the LHS and the resultant value will be assigned as the new value of the LHS..For example, If x=2 x+=3 This means that, x=x+3(RHS is added to LHS and assigned as the new value of LHS) So, substituting the initial value of x, x=2+3 Hence x=5,..5 is assigned as the new value of x Hope this helps....
Difference between += and + operators : (+=) | (+) ✓it is known as | ✓it is known as increment operator. | plus operator. ✓it is a assignment |✓it is arithmetic operator. | operator. [Common points] ✓ operator overloading is possible.
+= add the value and initialize it to the variable, a+=b is also a=a+b, and + is just and operator that add 2 values
+= is an assignment operator, while + is just an arithmetic operation. If you use +=, the value will be added to the variable. x = 5; x += 3; //x is now 8 x += 1 is the same as x = x + 1
let n = 5; n += 5; // similar as n = n + 5
Thanks everyone :D
let Consider int r=5; then int q=r+5; //q=5 r=+5 //(r=r+5) so r=10 similarly =*, =-, =/,
+ is arithmetic operator += is a short cut assignment operator the shortcut assignment operator is used only if there is a common variable in left and right hand side ex a=10 b=20; a=a+b; with += a+=b;