replace of one line counter loop with for-loop