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

Answer:

count = count + 2;

This adds two to the variable count.


More on the while Loop

Skip this page if you are clear how the while loop works. But study it if you still have some doubts.

  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" );

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

// start count out at one
int count = 1;       

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

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


QUESTION 4:

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

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