+ 5
What is the difference between += and +
10 ответов
+ 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
+ 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....
+ 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.
+ 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
+ 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
+ 4
let n = 5;
n += 5; // similar as n = n + 5
+ 1
Thanks everyone :D
0
let Consider int r=5;
then
int q=r+5; //q=5
r=+5 //(r=r+5) so r=10
similarly =*, =-, =/,
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;