fixed crash condition with for loop

fixed crash condition with for loop