Convert several iterator while loops to for loops

This removes confusion, limits scope and avoids bugs.

See this [stackexchange discussion](http://stackoverflow.com/questions/99164/iterate-with-for-loop-or-while-loop) for explanation.