Not a Counting Loop

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

The completed program is given below.

Not a Counting Loop

Here is the complete program. Copy it to a file and run it if you want to know how long it will take to reach a million. (Or wait for the answer in a few pages.)

class MillionDollarYears
{

  public static void main( String[] args )
  {
    double dollars = 1000.00 ;
    final  double interest = 0.05;
    int    year = 0;     

    while ( dollars < 1000000.00 )
    {
      // add another year's interest

      dollars = dollars + dollars*interest; 

      year    = year + 1;
    }

    System.out.println("It took " + year + " years to reach your goal.");
  }

}

This is not a counting loop, because the loop is being controlled by the result of a calculation. Only when the result reaches a goal is the loop finished. The variable year is a counter, but it is not controlling the loop.

QUESTION 4:

Why is this not a sentinel controlled loop?