12. Loop Control Variable


Answer:

The loop does not execute, even once. This is because the condition of the while statement,

    count <= limit

is false the first time. The statement following the loop,

    System.out.println( "Done with the loop" );

does execute.

Loop Control Variable

All the while loops in this chapter look something like this:

int count = 0;  
int limit = 5;
while ( count < limit )
{
System.out.println( "count is:" + count );
count = count + 1;
}
System.out.println( "Done with the loop" );

The variable count is initialized, tested, and changed as the loop executes. It is an ordinary int variable, but it is used in a special role. The role is that of a loop control variable. Not all loops have loop control variables, however.

The type of loop we have been looking at is a counting loop, since it counts upwards using the loop control variable as a counter. You can make counting loops with statements other than the while statement, and not all while loops are counting loops.


Question 12:

Do you think that a counting loop will always count upwards by ones?