[net.lang] using gotos

jonab@sdcrdcf.UUCP (05/31/84)

In article <1980@mit-eddie.UUCP> gds@mit-eddie.UUCP (Greg Skinner) writes:
>My feelings on the matter is that those are the only two conditions where a
>goto should be used (when trying to break out of a deeply-nested loop
>and when trying to handle some condition).

You can do both of these in the Ada* Programming Language quite easily
without using a single goto:

EXAMPLE 1:

procedure TEST;
    J: INTEGER;
begin
    ....
    LOOP1: loop
	....
	LOOP2: for I in 1..10 loop
	    ....
	    LOOP3: while j < 10 loop
		....
		exit LOOP1 when j = 5; -- exit all loops if j = 5
		....
		exit LOOP2;  -- unconditionally exit LOOP2
		....
		exit; -- exit innermost loop LOOP3
		....
	    end loop;
	    ....
	end loop;
	....
    end loop;
    ....
end TEST;

EXAMPLE 2:

with TEXT_IO; use TEXT_IO;
procedure COPY_FILE(INPUT: FILE_TYPE; OUTPUT: FILE_TYPE);
    C: character;
begin
    loop
	GET(INPUT, C);
	PUT(OUTPUT, C);
    end loop;
exception
    when END_ERROR =>        -- end of file
	null;                -- we are done, so do nothing
end COPY_FILE;

*Ada is a Trademark of the Ada* Joint Programming Office, Department of
 Defense.

Jon Biggar
{allegra,burdvax,cbosgd,hplabs,ihnp4,sdccsu3}!sdcrdcf!jonab

gds@mit-eddie.UUCP (Greg Skinner) (06/06/84)

[miss me ... i know you'll miss me]

I remember the time I was confronted by a decision whether or not to use
gotos -- I was programming in Pascal under VMS (no flames, please) and I
had to write a program that extracted various information from source
files.  I was used to programming in CLU which has very good exception
handling -- you can catch the end-of-file signal.  Anyway, Pascal under
VMS had no such exception handling, and I forgot.  My program would work
ok as long as it found its data, but when it was given programs where
the sought-after data was not there, it reached end-of-file and bombed
in a most undignified way (with one of those %PAS-E-ERRFIL or one of
those disgusting VMS error messages).

Well, I was in a dither, because I knew no way of handling this
end-of-file condition cleanly (I refused to put booleans in my code)
save by using a goto in front of every routine that accessed the file.
I was very dismayed, because I had always been taught "use of goto
considered harmful".  Anyhow, I sweated over this decision for about a
day or so, then went to my supervisor and begged permission to use
gotos.  Well, he just laughed at me for a while, and then said ok.
Since then, I haven't written any Pascal code, and have only used a goto
once (to exit from deep within a nested loop in a C program).  My
feelings on the matter is that those are the only two conditions where a
goto should be used (when trying to break out of a deeply-nested loop
and when trying to handle some condition).  Generally speaking, though,
it isn't a good thing to do.  As a matter of fact, the Pascal book I
used to teach myself (I forget the name but I think it was by Peter
Greenberg and I think it was called Programming in PASCAL) says that it
is ok to use gotos as handlers for exceptions such as end-of-file, but
nothing else.

-- 
                                                  Let fly the bits!

Greg Skinner (White Gold Wielder)
{decvax!genrad, eagle!mit-vax, whuxle, ihnp4}!mit-eddie!gds

And he who wields white wild magic gold is a paradox ...