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
  • 455

3 Answers3

7

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

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.
  • 13,093
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,718
  • 13
  • 19