Information Processing North-Holland
Letters
17 (1983) 47-51
IS AN EXIT STATEMENT
19 July 19X3
SUFFICIENT?
M.H. WILLIAMS Depcrrtmmt
o/ Conyuter
Scwnce, Herrot
Wutt Unroersrt_,, Edrrrhurgh.
EHl
2HJ,
Suxlrrnd
(L’. K.)
Communicated by W.L. Van der Poe1 Received February 1982 Keywrds:
Goto statement.
exit statement,
control
structures
Introduction The first control instruction to appear in a computer’s instruction set was the goto instruction (or jump or branch instruction, as it is variously called). This primitive but powerful instruction has changed very little during the past three decades and is still the basic control instruction at machine level to this day. When the first high level languages were developed, the concept of the goto instruction was carried over directly into these high level languages as the goto statement. At the same time three elementary forms of control structure were recognised: procedures, loops and choices (conditional statement or case statement). From the outset the problems associated with entering a procedure at any point other than the beginning were acknowledged and languages were designed to prevent the user from attempting to do so by placing restrictions on the scopes of label names. Exit from a procedure is less hazardous and some languages permit one to exit from one or more nested procedures by means of a goto statement. It was also recognised that a loop ought not to be entered through any point other than its first statement lest the control variable be set up incorrectly; but language designers had no strong feelings about this and restrictions were generally limited to verbal statements concerning the inadvisability of this manner of entry to loops, leaving it to the programmer to ensure that it did not happen. Exit from any point of the body of a loop 0020-0190/83/$3.00
0 1983, Elsevier Science Publishers
appeared harmless enough, and again the goto statement provided the means of achieving this. As far as the choice construct is concerned there was no apparent reason why entry to or exit from this construct should be restricted and so no restriction was placed on it. But as language designers sought after greater generality, even the restriction on entry to procedures was relaxed and the concept of multiple entry points for subroutines was introduced. This was the situation until the mid-1960s when the importance of simple program structures was realized and the idea of structured programming emerged. The fundamental tenet of the new doctrine is that the three control constructs (procedures, loops and choices) should each be restricted to have a single entry point and a single exit point, thereby making the structure of the program simpler. But it was also recognised that the use of structured programming did not always produce the most concise or efficient programs [ 11, and as a result language designers have been loathe to exclude the goto statement from their languages. Thus even the well-structured language PASCAL [2] includes a goto statement in its repertoire of instructions. This paper considers a generalized form of exit statement as an alternative to the goto statement. This statement is more restrictive than the goto statement but has the advantage that any program using it can easily be converted to a structured program.
B.V. (North-Holland)
41
The exit statement Consider
a programming language in which each has a single entry point (namely the beginning of the procedure body) and in which the normal exit point for a procedure is the end of the procedure body. Assume that there are various forms of loop statement but that each loop must be entered via its first statement. and that the normal exit from a loop occurs when the loop test is satisfied appropriately. Suppose that each conditional statement can only be entered via its initial condition and that exit normally takes place when execution of one of the option paths is completed. However, suppose that there is also an exit statement with the following two forms: procedure
(a)
Exrr(procedure
name).
When this statement is executed. its effect is to return control from the current procedure to the calling procedure, and from this to each successive calling procedure until the named procedure is reached. Execution continues from immediately after the most recently executed procedure call in this procedure.
Finally suppose that there is no goto statement (or its equivalent) in the language. The question is whether the exit statement can offer a reasonably simple and flexible alternative to the goto statement despite its constraints.
Some
specific
The first example is taken from the area of compiler construction [3]. In a recursive descent parser there are many points in the program at which an error may be dectected. In each case one may want to print an error message. abandon what has been accumulated thus far and search ahead for the next beacon symbol. Fig. la shows part of such a program written using a structured programming approach without a goto or exit statement. Fig. lb illustrates the same part rewritten using an exit statement. In practice. in a large recursive descent parser the overhead in space and time caused by the additional if statements, coupled with the resulting loss of clarity. make the
procedure statement; begin
var; if not err then
ExIT(label).
The label specified prefix a conditional the exit statement. to pass control to lowing the labelled Suppose further Boolean variables:
here must also be used to statement or a loop, enclosing The effect of this statement is the statement immediately folstatement. that there are three standard
end; procedure var; begin recid; if not err then if nextsym = leftround then restfncall else if nextsym = leftsquare then subscnpts end; procedure error; begin write
PKOCNOKMAL
~
LOOPNOKMAI.
~
CHOICENOKMAL
~
which is initially true. and is set to false whenever an exit statement causes an exit from a procedure, and to true whenever a normal procedure exit occurs, same for loops, same for choices (conditional statements or cases).
(‘error’);
err:= true
end; procedure master begin while not end do begin statement; if err then recover end end Fig. rxlt
4x
= asslgn then error begin lexan; if not err then expression end
if nextsym else
(b)
cases
la. Part of LI recurslvz statements.
descent
analyzer
uithout
goto
or
Volume
17. Number
I
INFORMATION
PROCESSING
procedure statement; begin var; if nextsym * assign then error else begin lexan; expression end end; procedure var; begin recid; if nextsym = leftround then restfncall else if nextsym = leftsquare then subscripts end; procedure error; begin write (‘error’); exit master end; procedure master: begin while not end do begin statement if not PROCNORMAL then recover end end Fig. lb. The same program statements.
as in Fig. la. rewritten
if k = 0 then goto done; unmove(a[k]); a[k]:= a[k] + 1; done
step 1 until n do if A[i] = x then goto found; notfound: n:= i; A[i]:=x; B[i]:=O; found : B[i]:= B[i] + 1;
This can be expressed statement as follows: loopl:
as a program
using an exit
for i:= 1 step 1 until n do
if A[i] = x then exit loop1 ; if loopnormal then begln n:= i; A[i]:=x;
B[i]:=O
end; B[i]:= B[i] + 1;
The third example is also taken from Knuth and is typical of a ‘backtracking’ application. The basic form of the program is as follows:
goto
try;
:
This may be written using an exit statement stead of a goto statement as follows:
in-
start: m[l]:=O; k:=O; : repeat k:= k + 1; list(k); a[k]:= m[k];
Ll
while a[k] sz m[k+ begin k:= k -
I] do
1;
if k = 0 then exit Ll ;
unmove(a[k]); a[k]:= a[k] + 1; end; move(a[k]) until false;
The
for i:=l
19 Julv 19X3
Start: m[l]:= 0; k:=O; up: k:= k+ 1; list(k), a[k]:= m[k]; try:if ark] < m[k+ l] then begin move (a[k]); goto up end; down : k:= k - 1;
using exit
structured programming approach very unattractive in this instance. As a result a goto statement is often used under these circumstances. An exit statement could equally well be used to produce the same effect. The second example is the search program described by Knuth and Floyd [l], viz.
LETTERS
general
approach
The goto statement as it appears in high level languages is usually restricted to jumping out of procedures or jumps within a procedure, but using a goto statement to jump into a procedure is not usually permissible. With this restriction in mind, any goto statement may be regarded as being made up of a sequence of zero or more returns from procedures, followed by a sequence of zero or more jumps out of loops and choices, followed by a simple jump which does not cross the boundary of any procedure. loop or choice construct. In other words it is equivalent to a possible procedure-type exit followed by a possible labeltype exit followed by a simple jump. The latter may be implemented as an if statement in the case of a forward jump, or as a loop (such as a repeat statement) in the case of a backward jump. The main difficulty when attempting to do without a goto statement lies in implementing unstructured forms. These are constructs which are not directly realizable with the normal tools of structured programming. The strict adherents of structured programming maintain that such con49
Volume
17, Numlxr
atructs
are
containing eliminate when clear,
I
INFORMATION
unnecessary
and
such constructs them. However.
that
any
PRO(‘F.SSING
LETTERS
algorithm
should be redesigned there are instances
to
[ 11
such constructs can be simple, efficient and and when a user may feel justified in using
them. It has been
shown
[4] that
unstructured forms. These (a) Ahnornwl .relec,tioc
there
are five basic
are the following: puth. This structure
illustrated in Fig. 2. It can be implemented an exit statement as follows: LAB:
ia using
if a then begin
B;
if b then begin
D; exit LAB end
else C end else A; if CONDNORMAL
then
E
(h) Loop kcith multiple exit points can be implemented as follows:
(Fig.
3). This can be implemented
flag LAB
:
repeat if
a
A;
LAB: if a then B else begin
then exit LAB;
repeat else
E
then
D else C
(c) Loop w?th nudtiple
Fig. 2. Flow diagram
entry points
of abnormal
selectmn
if CONDNORMAL
then C;
D; if b then flag + true
until flag; if LOOPNORMAL
A; exit LAB end;
flag + false;
B; if b then flag + true
50
as:
false;
else
E
until flag
(Fig. 4). This
path
Fig. 4. Flow diagram
of loop with
multiple
entry
point!.
Volume
17, Number
I
INFORMATION
PROCESSING
realized
19 Julv 1983
LETTERS
as follows:
LOOP : repeat
A;
if a then begin
C;
if c then exit LOOP; E end else begin
B;
if b then exit LOOP; D end
until false
Conclusion
Fig. 5. Flow diagram
of overlapping
loops.
(d) Overlapping loops (Fig. 5). This structure can be realized using an exit statement as follows: Ll
: repeat
A;
L2: repeat
B;
if a then exit L2; C; if b then exit Ll; E until false; D until false
(e) Parallel loops (Fig. 6). This structure
can be
The exit statement described here is a control construct which is more limited than the goto statement. However, by judicious combination with conditional statements and loops it can be used to produce the same effect as any goto statement. In particular the five basic unstructured forms can be implemented using exit statements. The chief advantage of the exit statement stems from its close relationship to the elements of structured programming, compared with the goto statement which bears no relation to structured programming. As a result a program employing exit statements but no goto statements can be transformed into a structured program relatively easily although the resulting program will inevitably be more cumbersome. A less important advantage is that the exit statement lends itself to one-pass compilation moreso than the goto statement in that it does not require labels to be defined at the beginning of each procedure or the main body. Whenever one encounters an exit statement which refers to a label, the definition of that label should have preceded it; otherwise the statement is in error. References [I] D.E. Knuth
Fig. 6. Flow diagram
of parallel
loops.
and R.W. Floyd, Notes on avoiding ‘Goto’ statements, Inform. Process. Lett. 1 (1971) 23-31. [2] C.A.R. Hoare and N. Wirth. An axiomatic definition of the programming language PASCAL. Acta Inform. 2 (1973) 335-355. [3] D.E. Knuth, Structured programming with goto statements, ACM Comput. Surveys 6 (1974) 261-301. [4] M.H. Williams, Generating structured flow diagrams: The nature of unstructuredness, Comput. J. 20 (I) (1977) 45-50.
51