Both StatementExpressionList
and LocalVariableDeclaration
are defined elsewhere on the page. I'll copy them here:
StatementExpressionList:
StatementExpression
StatementExpressionList , StatementExpression
StatementExpression:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
and
LocalVariableDeclaration:
VariableModifiers Type VariableDeclarators
VariableDeclarators:
VariableDeclarator
VariableDeclarators , VariableDeclarator
VariableDeclarator:
VariableDeclaratorId
VariableDeclaratorId = VariableInitializer
VariableDeclaratorId:
Identifier
VariableDeclaratorId [ ]
VariableInitializer:
Expression
ArrayInitializer
There's not much point in following the grammar any further; I hope it's easy enough to read as it is.
What it means is that you can have either any number of StatementExpressions
, separated by commas, or a LocalVariableDeclaration
in the ForInit
section. And a LocalVariableDeclaration
can consist of any number of "variable = value
" pairs, comma-separated, preceded by their type.
So this is legal:
for (int i = 0, j = 0, k = 0;;) { }
because "int i = 0, j = 0, k = 0
" is a valid LocalVariableDeclaration
. And this is legal:
int i = 0;
String str = "Hello";
for (str = "hi", i++, ++i, sayHello(), new MyClass();;) { }
because all of those random things in the initializer qualify as StatementExpressions
.
And since StatementExpressionList
is permitted in the update part of the for loop, this is valid too:
int i = 0;
String str = "Hello";
for (;;str = "hi", i++, ++i, sayHello(), new MyClass()) { }
Are you starting to get the picture?