I still don't get the assignment operator. How could x += be possible? How can you assign x equal to itself plus something? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I still don't get the assignment operator. How could x += be possible? How can you assign x equal to itself plus something?

Isn't it a mathematical paradox? Something can't be itself plus something else. Like 3 = 3 + 2. And what would assignment be useful for, in case it made sense?

9th Mar 2016, 6:25 PM
Angelo
Angelo - avatar
6 Answers
+ 3
x+= can be used for adding a value to a score in a game
11th Jun 2016, 5:59 PM
Alex Dodge
Alex Dodge - avatar
+ 3
lol this is kind of a philosophical question :) it could be a mathematical paradox, if it was (as you said) 3=3+2, but it's not mathematics, it's programming: you should consider it more as a grammar rule. It sounds weird, but it's just a way to say "increment x by 1". just... don't try a mathematical approach to grammar :D
15th Jun 2016, 12:53 PM
naghree
+ 2
x += x is just x = x + 1 so if int x = 4; x+=x; //x is now 5 later you learn you can use this to control loops in progams and i'm sure much much more.
11th Mar 2016, 11:36 PM
ravenXharuno
ravenXharuno - avatar
+ 2
Dont compare this assignment operator with mathematic paradoxes !!! X+=y means X=X+y ;;;; It is ambiguous tht why we dont prefer X=x+y in programming ::: U can check simply by using for loop if u write for( ; ;x=x+9) It will show problem in execution so instead for ( ; ;x+=9) Is used . . . Pls try :))
20th Apr 2016, 2:39 PM
Chirag Lathi
Chirag Lathi - avatar
+ 2
In mathematical terms , x=x+1 is not possible since the equation is incorrect. In programming point of view , x=x+1 is an assignment operation rather than equation . Here we increment the value of x by 1 and assign it to x . The value at the RHS of = (assignment operator ) is assigned to the variable at the LHS.
23rd Sep 2016, 7:16 PM
Hemprasanth Karunakaran
Hemprasanth Karunakaran - avatar
+ 2
all of the answers in this section are pretty good! in order to understand this we need to remember this is not a math class. This is programming, and java like all other programming languages have its rules x+=x according to java rules is the abbreviation for x=x+1. The way this works in java is think of the expression to the right of java, and evaluate, then whatever you get assign to the left. for example, if x had a value of 1 ...x+1 is 2 , now we store this two on the left so now x=2. think of this kind of expression of something that allows us to INCREMENT...same way ...x=x-1 allows us to "DECREMENT"
1st Oct 2016, 3:53 PM
Jose Ayala
Jose Ayala - avatar