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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

14.15 <strong>The</strong> break Statement BLOCKS AND STATEMENTS<br />

388<br />

Where a and i are compiler-generated identifiers that are distinct from any other<br />

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

the enhanced for statement occurs.<br />

DISCUSSION<br />

<strong>The</strong> following example, which calculates the sum of an integer array, shows how enhanced<br />

for works for arrays:<br />

int sum(int[] a) {<br />

int sum = 0;<br />

for (int i : a)<br />

sum += i;<br />

return sum;<br />

}<br />

Here is an example that combines the enhanced for statement with auto-unboxing to translate<br />

a histogram into a frequency table:<br />

Map histogram = ...;<br />

double total = 0;<br />

for (int i : histogram.values())<br />

total += i;<br />

for (Map.Entry e : histogram.entrySet())<br />

System.out.println(e.getKey() + "" + e.getValue() / total);<br />

14.15 <strong>The</strong> break Statement<br />

A break statement transfers control out of an enclosing statement.<br />

BreakStatement:<br />

break Identifieropt ;<br />

A break statement with no label attempts to transfer control to the innermost<br />

enclosing switch, while, do, or for statement of the immediately enclosing<br />

method or initializer block; this statement, which is called the break target, then<br />

immediately completes normally.<br />

To be precise, a break statement with no label always completes abruptly, the<br />

reason being a break with no label. If no switch, while, do, orfor statement in<br />

the immediately enclosing method, constructor or initializer encloses the break<br />

statement, a compile-time error occurs.<br />

A break statement with label Identifier attempts to transfer control to the<br />

enclosing labeled statement (§14.7) that has the same Identifier as its label; this<br />

DRAFT

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

Saved successfully!

Ooh no, something went wrong!