Java Reference
In-Depth Information
Of course, in this instance its effect is no different from that of an unlabeled break . However, in general
this would work wherever the labeled break statement was within OuterLoop . For example, it could be
nested inside another inner loop, and its effect would be just the same — control would be transferred to
the statement following the end of OuterLoop . The following code fragment illustrates this sort of situation.
The label this time is Outside :
Outside:
for(int i = 0 ; i<count1 ; ++i) {
...
for(int j = 0 ; j<count2 ; ++j) {
...
for(int k = 0 ; k<count3 ; ++k) {
...
break Outside;
...
}
}
}
// The labeled break transfers to here...
The labeled break is not needed very often, but when you need to break out of a deeply nested set of
loops, it can be invaluable because it makes it a simple operation.
ASSERTIONS
Every so often you will find that the logic in your code leads to some logical condition that should always
be true . If you test an integer and establish that it is odd, it is certainly true that it cannot be even, for ex-
ample. You may also find yourself writing a statement or statements that, although they could be executed in
theory, in practice they never really should be. I don't mean by this the usual sorts of errors that occur, such
as some incorrect data being entered somehow, which should be handled ordinarily by the normal code. I
mean circumstances where if the statements were to be executed, it would imply that something was very
seriously wrong with the program or its environment. These are precisely the circumstances to which asser-
tions apply.
NOTE Forassertionstohaveaneffectwhenyourunyourprogram,youmustspecifythe -en-
ableassertions option. For example:
java -enableassertions MyProg
You can also use its abbreviated form -ea:
java -ea MyProg
If you don't specify this option when you run the program, assertions are ignored.
Search WWH ::




Custom Search