GOTO

new topic     » goto parent     » topic index » view thread      » older message » newer message

I am a GW-BASIC Programming veteran.

I know how to use GOTO.
I know how not to use GOTO.
I know many of the messes I got myself into using GOTO.
I know how easy it is to use it and then forget how many
  times and from where I used it.

GOTO, as it was used in BASIC provides room to create
some very nasty code.  Just as others have said, The
problem with GOTO is that once you reach the label.
You don't really have anything telling you how you got
to the label.


Examine the following code.

SENTENCE$="This is a relatively short sentence."

INPUT B
IF B > 7 THEN GOTO Second
INPUT A
ON A GOTO First, Second
First:
A = A + B
Second:
PRINT MID$(SENTENCE$, A, 1)


Now assume that the code crashed at the PRINT MID$() command.
Did the error happen because of A = A + 5.  Or was that even
called?  Obviously this is a very small program example and
would be easy enough to track.  But assume that this is larger
program.  Say 200 lines or more.  Many variables.  Many
commands called.  You aren't really sure what the value of 'A'
or 'B' is at the point when it crashed.  And even if you do,
You don't really know how you reach the line that finally
crashed.  Also, That example only covers calling either
label from 1 or 2 places.  It is entirely possible for a
program to jump to a Label from many different places in a
program.

------------------
Let's consider nested for loop exit problem more carefully.
------------------
integer exit_flag

exit_flag = 0
for A = 1 to 50 do
  for B = 1 to 30 do
    for C = 1 to 75 do
      if get_key() != -1 then
        exit_flag = 1
        exit
      end if
    end for
    if exit_flag = 1 then
      exit
    end if
  end for
  if exit_flag = 1 then
    exit
  end if
end for

------------------
Why don't we consider suggesting this instead.
------------------
integer exit_flag

exit_flag = 0
for A = 1 to 50 do
  for B = 1 to 30 do
    for C = 1 to 75 do
      if get_key() != -1 then
        exit_flag = 1
        exit(A)-- or exit[A]
      end if
    end for
  end for
end for

------------------
In this case A isn't being used as a variable.
It is being used as a descriptor to a specific for
loop that has been automagically created thanks to Roberts
Genius creation called Euphoria.


This really does look like a much more pleasing alternative to
GOTO.

Do you want GOTO?
Do you think it should be limited to only 1 GOTO per LABEL?
Do you think it should ONLY go forward?
Do you think you should NOT be able to jump into program BLOCKS?
Do you think you should be allowed to jump out of program BLOCKS?
  Oops.  That sure kills jumping out of a for loop.
  Maybe jumping out of a for loop is to be allowed.
  Just can't jump out of a procedure or function.



PS: I consider myself to be very good at using/abusing GOTO.
    That is one reason I am so dead set against its introduction.
    PLEASE, consider other options.

        Lucius L. Hilley III
        lhilley at cdc.net   lucius at ComputerCafeUSA.com
+----------+--------------+--------------+
| Hollow   | ICQ: 9638898 | AIM: LLHIII  |
|  Horse   +--------------+--------------+
| Software | http://www.cdc.net/~lhilley |
+----------+-----------------------------+

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu