use for loop instead while loop