Loop Control Variable

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

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 about 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 the one that 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?