Can someone explain what exactly happens there and what the cons are of doing it with a while loop?
(will this cause delay when there are no messages, since if waits?)
No cons really. That's the way it's supposed to be handled:
You should process all pending messages before you run the game logic. Otherwise you will see the lagging that you described because the message queue will fill up with stale input data.