converting for loop to for each