Rewrite while loops