I see what happened now.
Yes, if the speed is negative and you make it negative, it becomes positive
if the speed is positive and you make it negative, it becomes negative, so it switches back and forth.
You could do it other ways, also, for instance, you could make a direction variable so you knew it was going backwards or forwards.
What happened with your other code is it reached > 500 only for one cycle and then it was less than 500 so it added to it again and didn't go anywhere. You needed to actually change the logic when it was going backwards so it kept moving to zero and at that point reversed the logic again so it kept moving toward 500. You were doing both operations at the same time so when it got to 500 they just alternated for one cycle.