Three Activities to Coordinate

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:

int count = 1;              // count is initialized
while ( count <= 3 )        // count is tested
{
  System.out.println( "count is:" + count );
  count = count + 1;        // count is changed
}
System.out.println( "Done with the loop" );      

Three Activities to Coordinate

Three activities of a loop must work together:

  1. The initial values must be set up correctly.
  2. The condition in the while statement must be correct.
  3. The change in variable(s) must be done correctly.

In the above program we wanted to print the integers "1, 2, 3". Three parts of the program had to be coordinated for this to work correctly.

QUESTION 7:

What will the program print if the initialization is changed as follows?

int count = 0;               // count is initialized
while ( count <= 3 )         // count is tested
{
  System.out.println( "count is:" + count );
  count = count + 1;         // count is changed
}
System.out.println( "Done with the loop" );