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. Below 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.
This is a result controlled loop because the loop stops only when an interest rate is found that achieves the goal we are seeking.
The number of times the loop iterates is not explicit in the program.
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" ); } }
The skeleton program (above) does not yet 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.)