JVM Knowledge

The rules herein cover the basic for statement (§14.14.1). Since the enhanced for statement (§14.14.2) is defined by translation to a basic for statement, no special rules need to be provided for it.

  • V is [un]assigned after a for statement iff both of the following are true:

    • Either a condition expression is not present or V is [un]assigned after the condition expression when false.
    • V is [un]assigned before every break statement for which the for statement is the break target.
  • V is [un]assigned before the initialization part of the for statement iff V is [un]assigned before the for statement.

  • V is definitely assigned before the condition part of the for statement iff V is definitely assigned after the initialization part of the for statement.

  • V is definitely unassigned before the condition part of the for statement iff both of the following are true:

    • V is definitely unassigned after the initialization part of the for statement.
    • Assuming V is definitely unassigned before the condition part of the for statement, V is definitely unassigned after the incrementation part of the for statement.
  • V is [un]assigned before the contained statement iff either of the following is true:

    • A condition expression is present and V is [un]assigned after the condition expression when true.
    • No condition expression is present and V is [un]assigned before the condition part of the for statement.
  • V is [un]assigned before the incrementation part of the for statement iff V is [un]assigned after the contained statement and V is [un]assigned before every continue statement for which the for statement is the continue target.

16.2.12.1. Initialization Part of for Statement

  • If the initialization part of the for statement is a local variable declaration statement, the rules of §16.2.4 apply.

  • Otherwise, if the initialization part is empty, then V is [un]assigned after the initialization part iff V is [un]assigned before the initialization part.

  • Otherwise, three rules apply:

    • V is [un]assigned after the initialization part iff V is [un]assigned after the last expression statement in the initialization part.
    • V is [un]assigned before the first expression statement in the initialization part iff V is [un]assigned before the initialization part.
    • V is [un]assigned before an expression statement S other than the first in the initialization part iff V is [un]assigned after the expression statement immediately preceding S.

16.2.12.2. Incrementation Part of for Statement

  • If the incrementation part of the for statement is empty, then V is [un]assigned after the incrementation part iff V is [un]assigned before the incrementation part.

  • Otherwise, three rules apply:

    • V is [un]assigned after the incrementation part iff V is [un]assigned after the last expression statement in the incrementation part.
    • V is [un]assigned before the first expression statement in the incrementation part iff V is [un]assigned before the incrementation part.
    • V is [un]assigned before an expression statement S other than the first in the incrementation part iff V is [un]assigned after the expression statement immediately preceding S.