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
forstatement 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
breakstatement for which theforstatement is the break target.
-
V is [un]assigned before the initialization part of the
forstatement iff V is [un]assigned before theforstatement. -
V is definitely assigned before the condition part of the
forstatement iff V is definitely assigned after the initialization part of theforstatement. -
V is definitely unassigned before the condition part of the
forstatement iff both of the following are true:- V is definitely unassigned after the initialization part of the
forstatement. - Assuming V is definitely unassigned before the condition part of the
forstatement, V is definitely unassigned after the incrementation part of theforstatement.
- V is definitely unassigned after the initialization part of the
-
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
forstatement.
-
V is [un]assigned before the incrementation part of the
forstatement iff V is [un]assigned after the contained statement and V is [un]assigned before everycontinuestatement for which theforstatement is the continue target.
16.2.12.1. Initialization Part of for Statement
-
If the initialization part of the
forstatement 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
forstatement 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.