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

Answer:

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

Outputs:

0 1 2 3 4 5 6 7 8 9 

Any Kind of Loop

Although the previous example is a counting loop, the for statement can be used to implement any of the three types of loops. The three parts, initialize, test, and change can be as complicated as you want. Here is a for loop with several statements in the loop body:

int count, sum;

sum = 0;
for ( count = 0;  count <= 5; count++ )
{
  sum = sum + count ;
  System.out.print( count + " " );
}
System.out.println( "\nsum is: " + sum );

Since the loop body consists of several statements, they are enclosed in braces { and } to make a block.


QUESTION 4:

What is the output of this new loop?