Any Kind of Loop

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:

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?