move check condition to outside in while block