Small Change

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.

Small Change

The change part can be complicated, if you want. It is best to keep it small and understandable. Here is almost the same loop as in the previous example, but now the control variable is incremented by two.

Recall (from Chapter 39) that count += 2 adds the value 2 to the variable count. Try to predict the output before you run the program.

int count;
for ( count = 0; count < 7; count += 2 )  
{
  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.




The change to count is done at the bottom of the loop body. This means that the last value that count gets is the first one that fails the test count < 7.

Questions like this are common on midterm and final examinations. If you rush, you are likely to get them wrong. But with careful thought they are easy enough.

QUESTION 7:

Read the description, and then fill in the blanks of the sequence. Then fill in the blanks of the loop that creates the same sequence.

description start at 1, count upward by 2's, quite when count exceeds 10
sequence
code for ( count = ; count ; )