Created the same loop using a while loop instead