Is there a java compiler flag that allows me to use goto
as a valid construct? If not, are there any third-party java compilers that supports goto
?
The goto
keyword is reserved but unused in the Java programming language. (From Section 3.9 of The Java Language Specification.)
Therefore, at least in the Java programming language, there is no way to enable the use of goto
.
However, as already noted, the goto opcode in the Java virtual machine is functional, and used when the Java compiler produces bytecode from the source.
Chapter 7: Compiling for the Java Virtual Machine from The Java Virtual Machine Specification may be of interest when implementing a JVM language.
The JVM bytecode contains a goto instruction (e.g. see the BCEL documentation).
Don't forget that Java itself supports the concept of jumping to labels, using:
break {labelname}
or
continue {labelname}
See this JDC tech tip for more info. If your language is compiled to JVM bytecode, then you may be able to make use of this.
Pretty much anything you can do with goto you can do with a loop. goto is really redundant and generally discredited way to program. IMHO.
If you want to goto backwards
LABEL: do {
// code before goto
// goto LABEL
continue LABEL;
// code after goto
break;
} while(true);
If you want to goto forwards
LABEL: do {
// code before goto
// goto LABEL
continue LABEL;
// code after goto
break;
} while(false);
// Label is effectively here
// code after LABEL.