move the condition outside the loop