go to previous page   go to home page   go to next page hear noise highlighting

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.


go to previous page   go to home page   go to next page