[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
GOTO
The goto statement is used to branch (jump) from one place to an-
other, within a function. The use of goto statements is considered
bad style. They can make code very hard to understand, and are in
fact almost never necessary. For example, Telix is written mainly in
the C language, which has a goto statement, yet except for a few
pieces of pre-written code, the goto statement was never used nor
needed. On the other hand, used very sparingly and properly, it can
sometimes make some code clearer and perhaps faster. The goto state-
ment consists of two parts, the 'label' or marker, which is where
execution will jump to, and the actual goto itself. A label is de-
fined in the form
<identifier>:
where <identifier> follows the same rules as for variable names.
Note that a colon follows the name, not a semicolon. The colon char-
acter must immediately follow the label name, with no intervening
spaces. A label does not have to be on a line by itself, and is not
considered a statement by itself. The goto takes the form
goto <label>;
where <label> is a label elsewhere in the function defined as de-
scribed above. Execution of the script will immediately continue
following the label.
An example is:
start:
prints("Hello");
goto start;
This would print the word "hello" over and over, forever. There is
no restriction on the placement of a label, so the above can be
written as:
start: prints("Hello");
goto start;
As mentioned above, there are usually better ways than using a goto
statement. For example:
int i = 0;
do
i = i + 1;
while (i < 100);
is clearer than the equivalent:
int i = 0;
loop:
i = i + 1;
if (i < 100)
goto loop;
One good use of a goto statement is to get out of a deeply nested
while statements, without having to do a lot of extra checking.
See Also:
break
continue
do
for
if
while
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson