for ( initialise ; test ; increment ) statement
initialise
and increment
usually involve assignments. Both can be comma-separated lists of statements.
initialise
(optional) sets initial conditions. It can include declaring variables, ie with their types - in that case the variables are local to the loop and no longer exist afterwards.
test
is a boolean condition: the loop executes as long as it is true.
increment
(optional) is executed after statement
, each time round the loop.
Both semicolons must always be present, even if optional parts are omitted.
for (int i = 0, j = 10; i < j; i++, j--)
{
if (3 * i > 2 * j)
{
System.out.println (i + " " + j);
}
}
In this example i and j are declared as part of the initialise
section and so they are local variables, available in the loop but not afterwards.