A good answer might be:

All three aspects of controling the loop,

  1. Initializing the loop,
  2. testing the ending condition, and
  3. changing the condition that is tested,
are set up in one statement. This makes it easier to check if you have done things correctly.

Declaring the Loop Control Variable

But there is another (smaller) aspect of loop control, and that is declaring the loop control variable. For example, here is a counting loop:

int count;

. . . . . .

for ( count = 0;  count < 10; count++ ) 
  System.out.print( count + " " );

The loop control variable count is declared somewhere else in the program (possible many statements away from the for statement.) It would be nice to include the declaration of count as part of the loop that uses it. In fact, this can be done, as in the following:

for ( int count = 0;  count < 10; count++ )
  System.out.print( count + " " );

In this example, the declaration of count and its initialization have been combined. However, there is an important difference between this program fragment and the previous one:

Important Note: a variable declared in a for statement can only be used in that statement and the body of the loop.
The following is NOT correct:

for ( int count = 0;  count < 10; count++ )    
  System.out.print( count + " " );

// NOT part of the loop body
System.out.println( "\nAfter the loop count is: " + count );  

Since the println statement is not part of the loop body, it cannot use count.

QUESTION 2:

Is the following code fragment correct?

int sum = 0;
for ( int j = 0;  j < 8; j++ )
    sum = sum + j;

System.out.println( "The sum is: " + sum ); 

Click Here after you have answered the question