How do I put a calculation inside a for loop with an if statement in between