What is the difference between += and + | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What is the difference between += and +

19th Jun 2019, 6:58 PM
Adham Khaled
Adham Khaled - avatar
9 Answers
+ 17
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
19th Jun 2019, 9:26 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 12
+ 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....
19th Jun 2019, 8:35 PM
Sharon Shelton
Sharon Shelton - avatar
+ 11
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.
20th Jun 2019, 3:17 AM
Manoj
Manoj - avatar
+ 6
+= 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
19th Jun 2019, 7:01 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 4
+= 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
19th Jun 2019, 7:01 PM
Airree
Airree - avatar
+ 4
let n = 5; n += 5; // similar as n = n + 5
20th Jun 2019, 1:37 AM
Ayan Fox
Ayan Fox - avatar
+ 1
Thanks everyone :D
19th Jun 2019, 9:15 PM
Adham Khaled
Adham Khaled - avatar
0
let Consider int r=5; then int q=r+5; //q=5 r=+5 //(r=r+5) so r=10 similarly =*, =-, =/,
20th Jun 2019, 7:16 AM
RAJAMANICKAM S
RAJAMANICKAM S - avatar
0
+ 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;
20th Jun 2019, 11:50 AM
sree harsha
sree harsha - avatar