Replace do while loop with while loop