use for-loop in place of while-loop