Why is goto bad?

Why is goto bad?

Like if I have some code such that:

TOP
hi please input x
if x does not meet requirement

goto TOP

why is this illegal?

Other urls found in this thread:

en.wikipedia.org/wiki/While_loop
twitter.com/AnonBabble

Because Dijkstra willed it, and thus it is truth

it's not a necessary pattern to use, and leads to mom's spaghetti

So what should be done instead if I want to start the code back from the beginning because for example the user put in an invalid input

Because you can use a while loop to get the same behavior. While loops are easier to recognize/understand than GOTOs, since a goto statement can take you all over the place, whereas a while loop is more limited and thus often less troublesome. Gotos often lead to spaghetti code but there's nothing inherently bad about them if you use them with restraint.

In Assembly you have to use goto as there's no
well-defined structure whatsoever.
In any other language you can use a decent structure to iterate your example code.

put the conditional in a while loop
while (x doesn't meet requirement)
input x
end while

en.wikipedia.org/wiki/While_loop

This. It's hard to follow the code execution when you have a bunch of GOTO instructions making it bounce around like a Mexican jumping bean.

you can also get input first with a do..while loop
do {
input x
} while (x == illegal)

i gotta say though, gotos make things a bit more cleaner
but if you can get too lazy then it devolves into spaghetti

The problem is that the goto if it refers to a line number can get fucked up if you add a line before that one.

>using BASIC
modern languages just use labels to jump to

10 OP
20 GOTO REDDIT

you can also use a break statement in an infinite loop
while (true) {
input x
if (x is valid) break
}

the bad thing about gotos is that it's easy to become disorganized with larger projects.

i mean i could, in theory, just stack all my personal documents in a pile on the floor instead of using a filing cabinet. For a small number of documents this is fine, maybe even preferable but with a larger number of files it'll be literally hell to navigate the pile.

A lot of programmers used it in the past, and most of them found out it leads to a lot of issues, even if you're careful.

They also found out it's completely unnecessary, you can do anything goto does with methods/loops/pointers.

Goto is not bad. It makes sense in a lot of place. Use it when it makes sense.

...

Nothing is wrong with goto but it is not compatible with modern language stuff. ASM/ML do that sort of thing a lot.

Even modern BASIC (like late 80s?) has labels.

It's not even that bad, makes a lot more sense when you're using big datashit too.

This is just another way to write a GOTO

It's a better way from a compiler standpoint because a compiler could see "Okay N iterations, lets optimize that down" or "okay infinite iterations, lets optimize that way instead."

Gotos are for people who do their own optimizations and they won't be doing them in C or BASIC with the goto keyword.

...

Goto are literally pointers. You can NOT make a computer without pointers. There is nothing wrong with goto

there's no finer way of getting out of nested loops

goto is just a jmp instruction, but unless you really know what you're doing its best to let the compiler optimize stack traversal and just use a loop.

I write programs in powershell for my job so I don't have labels and goto. must be nice.