converted while- to for-loop