double value ;
    int    tenths  = 0;
    int    inc     = 1;           // each "1" represents one tenth
     
    while ( tenths  <= 100 )   // 100 tenths is 10.0
    {
      value  = tenths/10.0 ;      // be sure to divide by a double 10.0
      System.out.println( "value:" + value );
      tenths =  tenths + inc ;
    }
    System.out.println( "done");

Use Integers for Counting

You might have wanted to use a loop control variable of primitive type double or float, and to add 0.1 to it each time. This would mostly work, but should be discouraged. Remember that 0.1 is not represented with complete accuracy inside a computer, and a loop that repeatedly adds 0.1 to some variable will rapidly accumulate noticible errors.

Just for fun, here is a program fragment that does just that. Enter various limit amounts and see how much error there is:

    // the user (you) enters a value for limit
    float inc   = 0.1 ;
    float value = 0.0 ;
    while ( value < limit )  
    {
      value = value + inc;
    }
    System.out.println( "Value was " + value + " when the loop ended");

Enter the limit value :

QUESTION 12:

If you enter a limit of 1000.0, what would be printed out if the arithmetic were perfectly accurate?

Click Here after you have answered the question