GOTO Statement

GOTO label ;

The GOTO statement causes a program to jump to a new statement in the program. When the GOTO statement is executed, the program jumps immediately to the statement with the given label and begin executing statements from that point. A label is a name followed by a colon that precedes an executable statement.

GOTO statements are often clauses of IF-THEN statements. For example, the following statements use a GOTO statement to iterate until a condition is satisfied:

start Iterate;
   x = 1;
   TheStart:
   if x > 10 then 
      goto TheEnd;
   x = x + 1;
   goto TheStart;

   TheEnd: print x;
finish;

run Iterate;

Figure 23.124 Iteration by Using the GOTO Statement
x
11

The function of GOTO statements is usually better performed by DO groups. For example, the preceding statements could be better written as follows:

x = 1;
do until(x > 10);
   x = x + 1;
end;

print x;

Figure 23.125 Avoiding the GOTO Statement
x
11

As good programming practice, you should avoid using a GOTO statement that refers to a label that precedes the GOTO statement; otherwise, an infinite loop is possible. You cannot use a GOTO statement to jump out of a module; use the RETURN statement instead.