0

goto statements can sometimes be useful to go down (to lower lines of code) in code, but can create a mess if used to go up (to higher lines of code). Therefore, I am wondering if there is any language that only allows goto statement to go down, e.g, godown.

Edit:

My main motivation for asking this question is this question Why does Go have a “goto” statement

I feel like if designers of a modern language like go decided to use goto statement there is a place for it. Also, as pointed out in one of the answers to the question, goto are used in go source code.

When I said "create a mess", I was referring to something like this, in a more complicated scenario:

package main

import "fmt"

func main(){
    i := 0
back:
    i++
    fmt.Println(i)
    if i < 10 {
        goto back
    }
    fmt.Println("we are finished")
}
Akavall
  • 425
  • 1
  • 3
  • 10
  • "but can create a mess if used to go up" - what do you mean? To higher placed lines of code (I can't see the difference)? Out of a block (then it's the opposite)? – John Dvorak Jan 08 '15 at 03:51
  • Are you looking for [goto docs](http://msdn.microsoft.com/en-us/library/aa664758(v=vs.71).aspx) from MSDN (on C#)? If so, the answer to your question is a trivial 'yes'. –  Jan 08 '15 at 03:54
  • 14
    Any sufficiently talented developer can create an equally incomprehensible mess with only downward gotos just as easily. – whatsisname Jan 08 '15 at 04:42
  • 5
    Most languages allow you to define subroutines in any order you want, or to switch the branches of a conditional around (by negating the condition), so any notion of "up" and "down" is arbitrary anyway, isn't it? – Jörg W Mittag Jan 08 '15 at 06:34
  • @JörgWMittag It's interesting to consider how things could be set up so that it's not arbitrary. – argyle Jan 08 '15 at 07:25
  • so if I want, for some reason, to change the order I'll get a compile error and will have to refactor the go-down logic? sounds worse than goto tbh :| – Thanos Tintinidis Jan 08 '15 at 17:43
  • Your "messy" example is actually a pretty neatly structured do-while loop. – Martin Maat Feb 15 '22 at 12:17
  • Maybe `throw` or `raise` exceptions in C++, Java, or Ocaml? – Basile Starynkevitch Feb 16 '22 at 06:54

3 Answers3

6

Some languages allow labelled break. This is a break that allows out of the loop of any nesting, like this

outer:
   for (var i = 0 ; i < M ; i++) {
       for ( var j = 0 ; j  < N ; j++) {
            if (f[i, j]) {
                break outer; 
            }
        }
    }

This can be seen as "goto only down and out", which is a far more restricted version of "goto only down".

Alex Guteniev
  • 199
  • 1
  • 3
  • In a similar vein, try/catch is also a veiled and more strictly regulated goto that only goes "down", so to speak. – Flater Feb 15 '22 at 18:47
  • @Flater, tom me the ability to go through function call stack, and the "exceptional" semantics make it distinct from "another goto". Although C `setjmp` / `longjmp` is somewhere in the middle between `goto` and exceptions. – Alex Guteniev Feb 16 '22 at 05:27
  • At least in C#, throws and and catches get translated to literal gotos. They're just compiler-managed rather than developer-managed. – Flater Feb 16 '22 at 09:20
2

I don't know of any [programming] languages that do this; if it's "bad" enough to allow goto at all then it will allow a goto to go anywhere.
I seem to recall a scripting language that only searched forward for the target of a goto statement but, sadly, I can't remember which one; it may have been a [very] early version of DOS.

Phill W.
  • 11,891
  • 4
  • 21
  • 36
  • 2
    JVM bytecode has `GOTO`, but it doesn't allow you to jump to anywhere. You can only a) jump a maximum of about 2 billion bytecode instructions forward or backward, and b) you can only jump to a location within the same method. – Jörg W Mittag Jan 08 '15 at 13:36
  • 1
    "if it's "bad" enough to allow goto at all then it will allow a goto to go anywhere": Well, no. If you are not allowed to go up you cannot construct any difficult to understand loops (I think this is what the OP meant by the possibility to "create a mess"), you can only skip a few lines in the body of your current function. IMO this kind of goto is not that bad, even though you can use higher level constructs such as `try-catch-finally` instead. – Giorgio Jan 08 '15 at 13:52
2

There are only few cases that I think an only-go-down goto statement might be useful, but all can be done more elegantly via break and switch statements.

There is no need for Goto-down statement, in my opinion, and I'm not aware of any mature or experimental programming language with such an ability.

53777A
  • 1,706
  • 13
  • 18