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")
}
8
  • "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)? Jan 8, 2015 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, 2015 at 3:54
  • 14
    Any sufficiently talented developer can create an equally incomprehensible mess with only downward gotos just as easily. Jan 8, 2015 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? Jan 8, 2015 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, 2015 at 7:25

3 Answers 3

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".

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 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. Feb 16 at 5: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 at 9: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.

2
  • 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. Jan 8, 2015 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, 2015 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.

0

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.