rewrite 'for' loops to declare iterator inside