added break condition and removed do loop