For performance and simplicity.
If a calculation does not depend on things inside the loop, you should ensure it happens before the loop. There are two reasons for this: simplicity and performance.
By extracting out concerns that don’t belong inside the loop to the outside, one can more easily identify them as belonging to the initialization or the iteration. Expanding and debugging programs is more straightforward for people unfamiliar with your code if you have made these things as clear as possible.
You can also improve performance by having as little logic inside the loop as possible. Performance generally should only be a concern if the program will run the loop a significant number of times; otherwise, you’ll just end up saving milliseconds that no one will notice.
Also, please move the stuff in the loop to a different method. That method should have minimal dependencies. This change will significantly reduce your method’s size and move the computation inside the loop to a separate concern.
Simplify your iterators. There are techniques where the iterator can be refactored out and take the loop calculation as a block argument. This approach is beneficial when your iterators appear several times in the code.