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

Answer:

count is: 1
count is: 3
count is: 5        after printing this, count is incremented by two and becomes 7
Done counting by two's.

The Gatekeeper

gatekeeper

The loop control variable count does not need to hit the limit exactly :

while ( count <= 6 ) 

In the above question, the value 5 passed the test and the loop body was executed. Then count was incremented to 7. Then 7 failed the condition of the while.

The condition part of a while is like a gatekeeper. It checks if execution is to be admitted into the loop body. It doesn't care how things came to be; if the test yields false, the loop body is skipped.

This is shown in the flowchart. Execution flows along the lines in the direction of the arrows. The diamond marked "Test Condition" represents the gatekeeper. If the condition tests true, execution enters the loop body. If the condition tests false, execution is not allowed into the loop body.

The loop body does some work and potentially changes the values in variables. Execution then returns to the top of the loop where the gatekeeper again decides whether to let it enter the loop body.


QUESTION 3:

Say that the test condition is count <= 6 and that there are many statements in the loop body.

If a statement somewhere in the loop body changes count to 12, does the loop body immediately stop execution? (Hint: look at the flow chart.)