for
StatementThe for
statement executes some initialization code, then executes an Expression, a Statement, and some update code repeatedly until the value of the Expression is false
.
ForStatement:
for (
ForInitopt;
Expressionopt;
ForUpdateopt)
Statement ForStatementNoShortIf:
for (
ForInitopt;
Expressionopt;
ForUpdateopt)
StatementNoShortIf ForInit:
StatementExpressionList
LocalVariableDeclaration ForUpdate:
StatementExpressionList StatementExpressionList:
StatementExpression
StatementExpressionList,
StatementExpression
The Expression must have type boolean
, or a compile-time error occurs.
for
statementA for
statement is executed by first executing the ForInit code:
for
statement completes abruptly for the same reason; any ForInit statement expressions to the right of the one that completed abruptly are not evaluated.for
statement. If execution of the local variable declaration completes abruptly for any reason, the for
statement completes abruptly for the same reason.for
statementNext, a for
iteration step is performed, as follows:
for
statement completes abruptly for the same reason. Otherwise, there is then a choice based on the presence or absence of the Expression and the resulting value if the Expression is present:
true
, then the contained Statement is executed. Then there is a choice:
for
statement completes abruptly for the same reason; 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.for
iteration step is performed.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.
for
statementAbrupt completion of the contained Statement is handled in the following manner:
break
with no label, no further action is taken and the for
statement completes normally.continue
with no label, then the following two steps are performed in sequence:
for
iteration step is performed.continue
with label L, then there is a choice:
for
statement has label L, then the following two steps are performed in sequence:
for
iteration step is performed.for
statement does not have label L, the for
statement completes abruptly because of a continue
with label L.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.