1

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")
}
  • "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 8 '15 at 3:51
  • Are you looking for goto docs from MSDN (on C#)? If so, the answer to your question is a trivial 'yes'. – user40980 Jan 8 '15 at 3:54
  • 10
    Any sufficiently talented developer can create an equally incomprehensible mess with only downward gotos just as easily. – whatsisname Jan 8 '15 at 4: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 8 '15 at 6:34
  • @JörgWMittag It's interesting to consider how things could be set up so that it's not arbitrary. – argyle Jan 8 '15 at 7:25
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.

  • 1
    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 8 '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 8 '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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.