Counting by Tenths

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   hear noise   go to next page

Answer:

No... sometimes things change by fractional amounts.

Counting by Tenths

Here is a program fragment that is intended to count upward by tenths, that is, 0.0, 0.1, 0.2, 0.3, and so on up to 10.0. These amounts are to be placed in the variable value.

double value ;
int tenths  = 0;
int inc     =   // pick an inc value
 
while ( tenths  <=  )   // put in the limit amount
{
  value =   // calculate the current value
  System.out.println( "value:" + value );
  tenths =  tenths + inc ;
}
System.out.println( "done");

This program is an example of a common situation: the actual value you are interested in is not the loop control variable, but a value that is calculated from the loop control variable.

QUESTION 12:

Fill in the blanks so that the variable value is assigned the values we want: 0.0, 0.1, 0.2, ... 1.0, 1.1, ... 9.8, 9.9, 10.0.

Hint: this is a somewhat tricky question. Notice that the inc variable is an integer, so cannot be initialized to 0.1.