Changed the way the foor loop works

Included the conditional check in the for loop