for keyword

Syntax

>>-for-(-+---------+-;-+------------+-;-+-----------+-)-Statement-><
         '-ForInit-'   '-Expression-'   '-ForUpdate-'

Description
The for statement executes some initialization code once, then executes an expression, a statement, and some update code repeatedly until the value of the expression is false. One of the primary uses of for loops is simple iteration, in which a block of code is repeated a certain number of times. The Expression must be type boolean; otherwise a compilation error occurs.

A for statement is executed by first executing the ForInit code.  Initialization code can contain statement expressions or local variable declarations.

After initialization, an iteration of the Statement is performed, as follows:

Abrupt Completion of for statement
Abrupt completion of the contained Statement is handled in the following manner:

Example
The following is a simple example of a for statement:

/*
 * The loop declares and initializes the value of x to 1.  
 * It then checks to see if the expression is satisfied; 
 * since x is 1, it is less than 5 and we can execute the 
 * body of the loop.
 *
 * Once the body is executed, the update code is executed.  In 
 * this case, we are incrementing x by 1 using the postfix 
 * increment operator.  After the update code is executed, we 
 * check the expression again.  If the expression is false, 
 * we stop executing the body of the loop and go to the next 
 * statement after the for loop.
 *
 * This loop will execute the body five times.
 */
int y;
for (int x = 1; x <= 5; x++) {
    y = x * 2;
    System.out.println (x + y);
} 

ngrelr.gif (548 bytes)
Syntax diagrams
Java types
boolean keyword

Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.