RE: About GOTO (was RE: fixed windows)

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

David Cuny wrote:
> 
> 
> Peter Williems wrote:
> 
> >    I'm not saying that goto is evil (although every informatics
> >    teacher will tell you) but the availability of a goto command
> >    generally does more bad than good, so there is a reason why many
> >    languages don't implement it.
> 
> I suspect the reasons are more pragmatic.
> 
> For example, I couldn't implement GOTO in the Eu (Euphoria written in 
> Euphoroia) because the code was stored in a tree structure. Since 
> Euphoria 
> won't give you the address of an arbitrary node, you can't just jump to 
> a 
> node - you have to traverse the tree. So coding Goto is non-trivial.
> 
> In other languages (Lua 4, FORTH) loop values are often placed on the 
> stack, 
> incremented and ultimately discarded by the END FOR construct. Jump out 
> of a 
> FOR loop, and you leave junk on the stack.
> 
> I can imagine that there might be some similar issues in Euphoria, where 
> an 
> arbitrary JUMP might cause a sequence not to be dereferenced properly. 
> (I 
> suspect that Robert's too clean a coder for that to be the case.)
> 
> The main reason that I can see for a GOTO is to get you out of a deeply 
> nested 
> routine. I've seen two interesting approaches to this.
> 
<snipped by biological entity>

Sorry for jumping in late, I'm way behind on my email reading.

It seems to me that a conditional goto, designed only for loop control 
and exiting, woould be much better than an unlimited "goto". Here's some 
pseudocode that outlines my thinking:

for i = 1 to 10
  -- imaginary "label A"
  <some code here>
  if <condition>
     redo -- acts like goto label A - repeats this pass thru
          -- the loop; index variable i doesn't change
  end if
  if <condition>
     loop -- acts like goto label B - skips the remainder of
          -- the code for this pass thru the loop
  end if
  if <condition>
     exit -- acts like goto label C - get out of loop NOW!
  end if
  -- imaginary "label B"
end for
-- imaginary "label C"

The exit, redo and loop syntax would give us more control than the 
simple existing "exit", without using the (apparently unpopular) goto.
The idea could be extended to nested loops, e.g.:

for i = 1 to 10 
  -- imaginary "label A"
  for j = 1 to 10
     -- imaginary "label B"
     <code> 
     if <condition>
        redo -- goto label B
     end if
     if <condition>
        redo i -- here's where it gets tricky: goto label A
               -- note the use of the "outer loop" index to
               -- identify where it is we're going
     end if
     if <condition>
        loop -- goto label C - there's no index identifier, so
             -- this "loop" is scoped to the containing j loop
     end if
     if <condition>
        exit -- goto label D
     end if
     if <condition>
        exit i -- goto label E
     end if
     -- imaginary label C
  end for
  -- imaginary label D
end for 
-- imaginary label E

This would also work for "while" loops, although identifying the "while" 
loop in nested code may be problematic:

for i = 1 to 10
   while 1 do
      while 1 do
         if <condition>
            exit 1 -- huh?
         end if
      end while
   end while
end for

Please bear in mind that the above is the opinion of a 30-something year 
COBOL accounting package developer...

Regards...  Tony B

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

Search



Quick Links

User menu

Not signed in.

Misc Menu