# for loop assistance

Printable View

• September 17th, 2012, 08:51 PM
Kseidel
for loop assistance
Hello all. I will not try to skip around and just say it flat out. I need help with some homework. I need to write two loops for a program, and right now I am attempting to code a for loop for the program that will calculate the value of an investment after so many years.
I have it all written, and the sytnax is good ( I think, I get no errors) but something in the logic of my code is wrong, because when I change how many iterations my loop has I get the same amount, or zero.
I have been told in learning programming that many times a second opinion can help, so if anyone can lend a hand, it would be most appreciated.

here is the loop I am using

Code Java:

```  for ( month = 1; month < 59; month++) { answer1 = (amount + (amount * 1.05)); }```

and here is the code

Code Java:

```  // This application performs a simple analysis on an growing investment   import javax.swing.JOptionPane; import java.text.DecimalFormat;   public class InvestGrowth { public static void main( String args[] ) { double answer1, amount; int answer2, month;   DecimalFormat twoDigits = new DecimalFormat( "0.00" );   // Assuming an investment of \$1000 at 5% per month, this // loop calculates the value of the investment after 5 years answer1 = 0.0; amount =(1000*1.05);   for ( month = 1; month < 59; month++) { answer1 = (amount + (amount * 1.05)); }   // Assuming an investment of \$1000 at 5% per month, this // loop calculates how long it will take until the investment // reaches \$1 million.   answer2 = 0;       JOptionPane.showMessageDialog(null, "\$1000 compounding a 5% per month is valued at \$" + twoDigits.format(answer1) + "\n" + "It takes " + answer2 + " months to reach \$1 million");   System.exit(0);   } // end main   } // end class BactGrowth```

To explain some things, I used only 59 months and started at 1 because I set amount at 1000 * 1.05 already, for the first months calculation instead of starting at 0 and 60.
• September 17th, 2012, 08:58 PM
jps
Re: for loop assistance
When you post, please be sure to ask a specific question. Short of reading a question, we are left to read every part of your program, try to determine what you want it to do, and try to determine where you went wrong. Asking a question saves much time.
• September 17th, 2012, 09:03 PM
Kseidel
Re: for loop assistance
I apologize, I have been at this for a while. Essentially I cannot get my for loop to work correctly and calculate the investment after so many iterations of the for loop, and when I try to change values of the amount of iterations, i get the same answer, or I get zero, so the only conclusion I can come to is that it is not doing all the iterations. My question is where did I go wrong and how can I fix it?
• September 17th, 2012, 09:10 PM
curmudgeon
Re: for loop assistance
Your loop keeps doing the same calculation over and over again. It never uses the result from the previous iteration (answer) in the next iteration. In other words answer is always on the left hand side of your expression in the loop, and never-changing amount is always on the right side of the expression in the loop.

You will want to learn how to
• Walk through your program as if you were the JVM and were doing the calculation to see why your code is not functioning right.
• do this calculation on paper first, then then reproduce those steps in your loop.