Optimize for loop statement