**New course!**Every coder should learn

**Generative AI!**

+ 8

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

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!!!

+ 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

+ 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)

+ 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.

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.

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.

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)

0

its like x=x+3 or x+=3... both means the same