Java Reference
In-Depth Information
10.10. What, No goto ?
The Java programming language has no goto construct that transfers
control to an arbitrary statement, although goto is common in languages
to which the language is related. [2] The primary uses for goto in such lan-
guages have other solutions:
[2] Although not used goto is a reserved keyword, as is const . The reason they are reserved is mostly
historical: Both of these come from strongly related programming languages, like C and C++, and re-
serving them helped compilers advise programmers clearly that they were using something that didn't
make sense. Occasionally, suggestions are made for how const might be used in the Java program-
ming language.
Controlling outer loops from within nested loops. Use labeled break
and continue statements to meet this need.
Skipping the rest of a block of code that is not in a loop when an
answer or error is found. Use a labeled break .
Executing cleanup code before a method or block of code exits.
Use either a labeled break or, more cleanly, the finally construct
of the try statement covered in Chapter 12 .
Labeled break and continue have the advantage that they transfer control
to a strictly limited place. A finally block is even stricter as to where it
transfers control, and it works in all circumstances, including exceptions.
With these constructs you can write clean code without a goto .
Furious activity is no substitute for understanding.
H.H. Williams
 
 
Search WWH ::




Custom Search