I don't get how x=x+3 can be written as x+=3. Can someone please explain this to me better? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

I don't get how x=x+3 can be written as x+=3. Can someone please explain this to me better?

26th Oct 2015, 8:34 PM
DanielXR
DanielXR - avatar
8 Answers
+ 26
It's simple, When you are working as a professional developer you would got to know why and how do we use it. x+=2; // means x=x+2; x-=2; // means x=x-2; x*=2; // means x=x*2; x÷=2; // means x=x÷2; x%÷2; // means x=x%2; These are called shorthand operators. You might think what's the use of this? we can write it in simple form. Well, suppose you are working on a code and as int totalNumberOfMSStudentsInNewYorkCity=1000; suppose you want to add 5 more students then what method will you use? here, the shorthand method is more efficient. totalNumberOfMSStudentsInNewYorkCity+=5; so, we didn't need to write the variable twice. but this was the simplest example, imagine a program where you need to use such long named variables often. Hope you understood!!!
26th Oct 2016, 9:06 AM
Taha Ansari
Taha Ansari - avatar
+ 9
essentially it's to save space or not type "x" twice. Not a big deal here since it's just "x" but you might have something a longer variable name. Youre code will save whatever "x" is from your last operation and increment it by 3. if x = 1 next time it will be 4. next time it will be 7 and so on
29th Oct 2015, 4:56 PM
1010101
+ 7
Because that's not a mathematical expression :) and it's like telling to the computer which operation to do firstly (addition) and then which is the second (assigning the result to the variable x)
12th Oct 2016, 7:33 AM
Klodian Lula
Klodian Lula - avatar
+ 1
At first when you entered an integer to variable x, that integer got stored in the memory location x. So next time when you use var x, the value stored in its memory location will be manipulated depending on the operators you use. Hope that makes sense. The operator you would like to use is put with = sign, and the var x (a value/integer in this var defined before) comes before '<operator>=' notation and the number which you want to carry out an arthimetic operation with x is put after that notation.
7th Mar 2016, 10:06 PM
Sohail Ziahh
Sohail Ziahh - avatar
0
the first x is the new variable and the second one is the older one, which is here 2 so thus..x=x+3 that is x=2+3 = 5. so this value (5) will be replaced with (2) in x variable.
24th Feb 2016, 9:08 AM
Meghraj Solanki
Meghraj Solanki - avatar
0
X=x+3 is an accumulator , x+=3 can be read as: variable x will be + incremented as specified after the = signal. Maybe reading in that way may make more sense.
13th Nov 2016, 3:32 AM
Lalo alarmas
Lalo alarmas - avatar
0
If you like, you still can use x = x + 3. The result will be the same as x += 3 . The later is just a shortcut. I like more x = x + 3 (so far)
9th Jan 2017, 6:00 AM
Guillermo
0
its like x=x+3 or x+=3... both means the same
12th Jan 2017, 5:27 PM
seetha
seetha - avatar