0
I don't understand the logic behind swapping without temp var, plz help.
Swapping with temp is clear to me, but not without it. Plz enlighten me :) https://code.sololearn.com/c32oZAA2T3Zd/?ref=app
2 Respuestas
+ 1
x = x + y;
y = x - y;
x = x - y;
it's a simple subtraction.
suppose x=3 and y = 5
x = 3 + 5 // x is now 8
y = 8 - 5 // y is now 3 the value of x before
x = 8 - 3 // x is now 5 the value of y
whenever something like this is confusing you just do a substitution of the variables
may be you are confused because you think that:
x = 3
x = 3 + 5 it's not equivalent to 3 = 3 +5
the = sign is an assignment operator not equal. could be wrong if so what confused you?
+ 1
So simple and genius, that was my stumbling block. This way it's so much clearer. Thanks mate!