replacing if with while