I have a for loop that adds on a monthly interest rate to two different account balances and prints the new balance for each month 0 - 12. (this part works great!) After it goes through the process I am suppose to print the balance of the accounts added together. The problems is when I do this, I get an extra months worth of interest for both accounts in the total.
Here is my "For loop" code with the subsequent print statement:
print the values 13 times
add the interest 12 times.
So having a loop with a counter of 13 with both printing and adding interest is going to get you an extra month of interest. Your loop should only have a counter of 12. Then, depending on exactly how you write it, you could either
Either one would get you 12 months of interest, and print the values 13 times. It's actually pretty common to do things this way.
I prefer the earlier solution with a statement outside the loop.
Joined: Jul 20, 2008
@Campbell - I was just trying to modify the existing code here
But yes I agree, a statement outside the for loop, before the loop actually starts is more readable and even the for loop will then work for the purpose it is required to fulfill - calculating interest for 12 months.