replace 'while' loop with 'for' loop