Answer:

Now show the for statement for each of the following sequences

start at 0, count upward by 1's, end at 7 for ( count = 0; count < 8; count++ )
start at 0, count upward by 2's, end at 14 for ( count = 0; count < 15; count += 2 )
start at 1, count upward by 2's, end at 15 for ( count = 1; count < 16; count += 2 )

Incrementing by a Variable

Here is a program fragment and its JavaScript equivalent that uses an increment amount contained in a variable. Try to predict what will be output each time you run the program.

int inc;

// ... get inc from the user ...

for ( count = 0; count < 21; count = count + inc )  
{
  System.out.println( "count is: " + count ); 
}
System.out.println( "\nDone with the loop.\nCount is now" + count);
Here is a JavaScript version of this loop.




Enter increment value:

QUESTION 9:

What is the smallest value of inc such that the loop body will execute only one time? Confirm your answer by testing it with the program.