Convert to for-loops. Patch based on part 3 from Graham Russell. Contains a few whitespace changes. Bugzilla Id: 57981