This works. What my question would be is that why does this work? Wouldn't you need the else statements after the second if statement for each x and y? because when I tried that it didn't work. but this does. I'm just curious why.
Well, I'm guessing this snippet of code is located in some callback function.
Let's say that you press and hold the right arrow key. Your velocityX is now 200.0f units of whatever.
You let go of the right arrow key, and your velocityX is now zero. It's zero, because the next time the callback function gets called (which, I'm assuming is every other frame), it's very likely that you're not pressing left. Because you're not pressing left, the else blocks get executed and set the velocity to zero.
If you were to put an else after each if, that wouldn't work for the following reason:
Let's say you're pressing the up arrow key. Your velocityY is now -200.0f, but then right after you're checking if you're going down, which you're not, so velocityY gets set to zero.