replace the for loop by a while loop