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.
- The variable
countis assigned a 1. - The condition
( count <= 3 )is evaluated as true. - Because the condition is true, the block statement following the while
is executed.
- The current value of
countis written out: count is 1 countis incremented by one, to 2.
- The current value of
- The condition
( count <= 3 )is evaluated as true. - Because the condition is true, the block statement following the while
is executed.
- The current value of
countis written out. count is 2 countis incremented by one, to 3.
- The current value of
- The condition
( count <= 3 )is evaluated as true. - Because the condition is true, the block statement following the while
is executed.
- The current value of
countis written out. count is 3 countis incremented by one, to 4.
- The current value of
- The condition
( count <= 3 )is evaluated as FALSE. - Because the condition is FALSE, the block statement following the while is SKIPPED.
- The statement after the entire while-structure is executed.
- System.out.println( "Done with the loop" );
QUESTION 4:
- How many times was the condition true?
- How many times did the block statement following the
whileexecute?