10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

BLOCKS AND STATEMENTS <strong>The</strong> enhanced for statement 14.14.2<br />

◆ If the for statement does not have label L, the for statement completes<br />

abruptly because of a continue with label L.<br />

• If execution of the Statement completes abruptly for any other reason, the for<br />

statement completes abruptly for the same reason. Note that the case of abrupt<br />

completion because of a break with a label is handled by the general rule for<br />

labeled statements (§14.7).<br />

14.14.2 <strong>The</strong> enhanced for statement<br />

<strong>The</strong> enhanced for statement has the form:<br />

EnhancedForStatement:<br />

for ( VariableModifiersopt Type Identifier: Expression) Statement<br />

<strong>The</strong> Expression must either have type Iterable or else it must be of an array<br />

type (§10.1), or a compile-time error occurs.<br />

<strong>The</strong> scope of a local variable declared in the FormalParameter part of an<br />

enhanced for statement (§14.14) is the contained Statement<br />

<strong>The</strong> meaning of the enhanced for statement is given by translation into a<br />

basic for statement.<br />

If the type of Expression is a subtype of Iterable, then let I be the type of the<br />

expression Expression.iterator(). <strong>The</strong> enhanced for statement is equivalent to<br />

a basic for statement of the form:<br />

for (I #i = Expression.iterator(); #i.hasNext(); ) {<br />

VariableModifiersopt Type Identifier = #i.next();<br />

Statement<br />

}<br />

Where #i is a compiler-generated identifier that is distinct from any other identifiers<br />

(compiler-generated or otherwise) that are in scope (§6.3) at the point where<br />

the enhanced for statement occurs.<br />

Otherwise, the Expression necessarily has an array type, T[]. Let L1 ... Lm<br />

be the (possibly empty) sequence of labels immediately preceding the enhanced<br />

for statement. <strong>The</strong>n the meaning of the enhanced for statement is given by the<br />

following basic for statement:<br />

DRAFT<br />

T[] a = Expression;<br />

L1: L2: ... Lm: for (int i = 0; i < a.length; i++) {<br />

VariableModifiersopt Type Identifier = a[i];<br />

Statement<br />

}<br />

387

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!