As before the advantage of the enhanced for is a simple, syntactic construct for accessing the elements you
are iterating through. But you cannot use the iterator's remove method to alter the collection, nor can you
iterate through multiple collections at the same time. If you need these capabilities you must use the basic
for statement and an explicit iterator.
10.6. Labels
You can label statements to give them a name by which they can be referred. A label precedes the statement it
names; only one label is allowed per statement:
label: statement
Labels can be referred to only by the break and continue statements (discussed next).
10.7. break
A break statement can be used to exit from any block, not just from a switch. There are two forms of
break statement. The unlabeled break:
break;
and the labeled break:
break label;
An unlabeled break terminates the innermost switch, for, while, or do statementand so can appear
only within one of those statements. A labeled break can terminate any labeled statement.
A break is most often used to break out of a loop. In this example, we are looking for the first empty slot in
an array of references to Contained objects:
class Container {
private Contained[] objs;
// ...
public void addIn(Contained obj)
throws NoEmptySlotException
{
int i;
for (i = 0; i < objs.length; i++)
if (objs[i] == null)
break;
if (i >= objs.length)
throw new NoEmptySlotException();
objs[i] = obj; // put it inside me
obj.inside(this); // let it know it's inside me
}