Convert loops from "for" to "while"