changes for loop to a while loop