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.
- If the ForInit code is a list of statement expressions, the expressions are
evaluated in sequence from left to right and their values, if any, are discarded. If
evaluation of any expression completes abruptly for some reason, the for
statement completes abruptly for the same reason and any ForInit statement
expressions to the right of the one that completed abruptly are not evaluated.
- If the ForInit code is a local variable declaration, it is executed as if it
were a local variable declaration statement appearing in a block. In this case, the scope
of a declared local variable is its own initializer and any further declarators in the ForInit
part, plus the Expression, ForUpdate, and contained Statement
of the for statement. If execution of the local variable declaration
completes abruptly for any reason, the for statement completes abruptly for
the same reason.
- If the ForInit part is not present, no action is taken.
After initialization, an iteration of the Statement is performed, as
follows:
- If the Expression is present, it is evaluated, and if evaluation of the Expression
completes abruptly, the for statement completes abruptly for the same reason.
- If the Expression is not present, or if it is present and its value is true,
then the contained Statement is executed. After that, a decision is made, based
on the following rules:
- If execution of the Statement completes normally, then the following two
steps are performed in sequence:
- First, if the ForUpdate part is present, the expressions are evaluated in
sequence from left to right; their values, if any, are discarded. If evaluation of any
expression completes abruptly for some reason, the for statement completes
abruptly for the same reason and any ForUpdate statement expressions to the
right of the one that completed abruptly are not evaluated. If the ForUpdate
part is not present, no action is taken.
- Second, another for iteration step is performed.
- If execution of the Statement completes abruptly, see the section on Abrupt Completion of for statement below.
- If the Expression is present and its value is false, no further action is
taken and the for statement completes normally.
- If the value of the Expression is false the first time it is evaluated, then
the Statement is not executed.
- If the Expression is not present, then the only way a for
statement can complete normally is by use of a break statement.
Abrupt Completion of for
statement
Abrupt completion of the contained Statement is handled in the following
manner:
- If execution of the Statement completes abruptly because of a break
with no label, no further action is taken and the for statement completes
normally.
- If execution of the Statement completes abruptly because of a continue
with no label, then the following two steps are performed in sequence:
- First, if the ForUpdate part is present, the expressions are evaluated in
sequence from left to right; their values, if any, are discarded. If the Update
part is not present, no action is taken.
- Second, another for iteration step is performed.
- If execution of the Statement completes abruptly because of a continue
with label L, a decision is made, based on the following rules:
- If the for statement has label L, then the following two steps are
performed in sequence:
- First, if the ForUpdate part is present, the expressions are
evaluated in sequence from left to right; their values, if any, are discarded. If the ForUpdate
is not present, no action is taken.
- Second, another for iteration step is performed.
- If the for statement does not have label L, the for
statement completes abruptly because of a continue with label L.
- If execution of the Statement completes abruptly for any other reason, the for
statement completes abruptly for the same reason. Note that the case of abrupt completion
because of a break with a label is handled by the general rule for labeled
statements.
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);
}

Syntax diagrams
Java types
boolean keyword
Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.
