More on the while Loop

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:

10 times

More on the while Loop

Here is the part of the program responsible for the loop:

int count = 1;                                  // start count out at one
while ( count <= 3 )                            // loop while count is <= 3
{
  System.out.println( "count is:" + count );
  count = count + 1;                            // add one to count
}
System.out.println( "Done with the loop" );

Here is how it works in tedious detail. Look especially at steps 7, 8, and 9.

  1. The variable count is assigned a 1.
  2. The condition ( count <= 3 ) is evaluated as true.
  3. Because the condition is true, the block statement following the while is executed.
    • The current value of count is written out:    count is 1
    • count is incremented by one, to 2.

  4. The condition ( count <= 3 ) is evaluated as true.
  5. Because the condition is true, the block statement following the while is executed.
    • The current value of count is written out.   count is 2
    • count is incremented by one, to 3.

  6. The condition ( count <= 3 ) is evaluated as true.
  7. Because the condition is true, the block statement following the while is executed.
    • The current value of count is written out.   count is 3
    • count is incremented by one, to 4.

  8. The condition ( count <= 3 ) is evaluated as FALSE.
  9. Because the condition is FALSE, the block statement following the while is SKIPPED.

  10. The statement after the entire while-structure is executed.
    • System.out.println( "Done with the loop" );

QUESTION 4:

  1. How many times was the condition true?
  2. How many times did the block statement following the while execute?