Optimized unnecessary iteration in cycle with a break