replace if with while