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

Answer:

No. The condition is only tested just before execution enters the loop body. Once execution enters the loop body, anything can happen. Of course, the condition will be tested again after the loop body has executed.


Live Code!

Here is some code that illustrates changing the loop control variable by an amount other than one.

Enter the initial value for count and a value for the increment.

(The output window sometimes shows messages that would not be printed by the example code.)


int count =  ;

int increment =  ;

while ( count <= 12 )
{
  System.out.println( "count is:" + count );
  count = count + increment;
}

System.out.println( "Count was " + count 
    + " when it failed the test");
       

QUESTION 4:

Puzzles:

Find an initial value for count and a value for increment so that:

1. the loop body is only executed once.

2. the loop body is executed twice.

3. the loop body is not executed at all.

4. the loop goes on forever.

5. the loop body prints only "6" .

(Remind me to put this question on the midterm.)