In the above dialog, the user keeps trying interest rates until one is found where the account reaches the million dollar goal. In terms of computer programming, what is the user doing?
The user is acting like a result-controlled loop.
The user is acting like a result-controlled loop. We can write the program do this. Here is an outline of a program that starts the interest rate out at zero percent, then increases it in tenth percent increments until the million dollar goal is reached:
class MillionDollarInterestRate { public static void main( String[] args ) { double initialAmount = 1000.00 ; double dollars = 0.0; double rate = -0.001; // This is about to change while ( dollars < 1000000 ) { // change to the next rate rate = rate + 0.001; // compute the dollars after 40 years at the current rate . . . . . . } System.out.println("After 40 years at " + rate*100 + " percent interest you will have " + dollars + " dollars" ); } }
This is a result controlled loop, because the loop stops only when the interest rate is found that gives us the result we want. The number of times the loop iterates is not explicit in the program.
The skeleton program does not have code for the part that says "compute the dollars after 40 years at the current rate." How do you expect that this will be done? (Hint: look at the previous version of the program.)