nit: you can replace while loop with do ... while and remove this line.