Questions tagged [goto]

In imperative programming, a "go to" statement is an unconditional jump instruction that changes the flow of control to the point of the program referenced by the "go to" statement.

1057 questions
310
votes
49 answers

GOTO still considered harmful?

Everyone is aware of Dijkstra's Letters to the editor: go to statement considered harmful (also here .html transcript and here .pdf) and there has been a formidable push since that time to eschew the goto statement whenever possible. While it's…
Kyle Cronin
  • 77,653
  • 43
  • 148
  • 164
296
votes
14 answers

Is there a "goto" statement in bash?

Is there a "goto" statement in Bash? I know it is considered bad practice, but I need specifically a "goto".
kofucii
  • 7,393
  • 12
  • 51
  • 79
288
votes
23 answers

Is there a goto statement in Java?

I'm confused about this. Most of us have been told that there isn't any goto statement in Java. But I found that it is one of the keywords in Java. Where can it be used? If it can not be used, then why was it included in Java as a keyword?
Venkat
  • 20,802
  • 26
  • 75
  • 84
280
votes
22 answers

Is there a label/goto in Python?

Is there a goto or any equivalent in Python to be able to jump to a specific line of code?
user46646
  • 153,461
  • 44
  • 78
  • 84
227
votes
6 answers

What is wrong with using goto?

Possible Duplicates: Why is it bad to use goto? GOTO still considered harmful? I was ramdomming through xkcd and saw this one (if also read some negative texts about them some years ago): What is actually wrong with it? Why are goto's even…
user142019
224
votes
26 answers

Are there any legitimate use-cases for "goto" in a language that supports loops and functions?

I've long been under the impression that goto should never be used if possible. However, while perusing libavcodec (which is written in C) the other day, I was surprised to notice multiple uses of it. Is it ever advantageous to use goto in a…
Landon
  • 15,166
  • 12
  • 37
  • 30
162
votes
17 answers

How can I use goto in Javascript?

I have some code that I absolutely must implement using goto. For example, I want to write a program like this: start: alert("RINSE"); alert("LATHER"); repeat: goto start Is there a way to do that in Javascript?
Peter Olson
  • 139,199
  • 49
  • 202
  • 242
156
votes
3 answers

Why does Go have a "goto" statement?

I was surprised to find that Go has a 'goto' statement. I've always been taught that 'goto' statements are a thing of the past and evil for it occludes the actual flow of a program, and that functions or methods are always a better way of…
harm
  • 10,045
  • 10
  • 36
  • 41
127
votes
8 answers

Does anyone still use [goto] in C# and if so why?

I was wondering whether anyone still uses the "goto" keyword syntax in C# and what possible reasons there are for doing so. I tend to view any statements that cause the reader to jump around the code as bad practice but wondered whether there were…
Brian Scott
  • 9,221
  • 6
  • 47
  • 68
107
votes
16 answers

Valid use of goto for error management in C?

This question is actually a result of an interesting discussion at programming.reddit.com a while ago. It basically boils down to the following code: int foo(int bar) { int return_value = 0; if (!do_something( bar )) { goto error_1; …
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
99
votes
1 answer

Will using goto leak variables?

Is it true that goto jumps across bits of code without calling destructors and things? e.g. void f() { int x = 0; goto lol; } int main() { f(); lol: return 0; } Won't x be leaked?
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
86
votes
12 answers

Alternative to a goto statement in Java

What is an alternative function for the goto keyword in Java? Since Java does not have a goto.
gmhk
  • 15,598
  • 27
  • 89
  • 112
81
votes
7 answers

Variable declaration after goto Label

Today I found one interesting thing. I didn't know that one can't declare a variable after a goto label. Compiling the following code #include int main() { int x = 5; goto JUMP; printf("x is : %d\n",x); JUMP: int a = 0; …
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
79
votes
16 answers

Examples of good gotos in C or C++

In this thread, we look at examples of good uses of goto in C or C++. It's inspired by an answer which people voted up because they thought I was joking. Summary (label changed from original to make intent even clearer): infinite_loop: // code…
fizzer
  • 13,551
  • 9
  • 39
  • 61
77
votes
11 answers

Is GOTO in PHP evil?

I recently found out that PHP 5.3 supports new language construct called GOTO. Everybody knows what it does. However, it's not exactly the traditional GOTO, it's just a jump label. I'm interesting in knowing whether this GOTO is evil and implies bad…
Tower
  • 98,741
  • 129
  • 357
  • 507
1
2 3
70 71