+ 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?
6 Answers
+ 3
x+= can be used for adding a value to a score in a game
+ 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
+ 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.
+ 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 :))
+ 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.
+ 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"