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
24
votes
11 answers

How to store goto labels in an array and then jump to them?

I want to declare an array of "jumplabels". Then I want to jump to a "jumplabel" in this array. But I have not any idea how to do this. It should look like the following code: function() { "gotolabel" s[3]; s[0] = s0; s[1] = s1; s[2]…
youllknow
24
votes
3 answers

'goto *foo' where foo is not a pointer. What is this?

I was playing around with labels as values and ended up with this code. int foo = 0; goto *foo; My C/C++ experience tells me *foo means dereference foo and that this won't compile because foo isn't a pointer. But it does compile. What does this…
Sarvadi
  • 550
  • 3
  • 13
24
votes
7 answers

c99 goto past initialization

While debugging a crash, I came across this issue in some code: int func() { char *p1 = malloc(...); if (p1 == NULL) goto err_exit; char *p2 = malloc(...); if (p2 == NULL) goto err_exit; ... err_exit: …
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
24
votes
2 answers

Same goto labels used in a C file but different functions

Can someone please tell me if it is acceptable to use same goto labels in different functions in the same C file? To explain what I am facing: function1() { ... goto label ... label: ... } function2() { ... goto label; …
user2311285
  • 437
  • 6
  • 14
23
votes
10 answers

Is GOTO considered harmless when jumping to cleanup at the end of function?

The goto statement has been examined at great length in several SO discussions (see this and that), and I certainly don't want to revive those heated debates. Instead, I'd like to concentrate on a single use case of gotos and discuss its value and…
Philip
  • 5,795
  • 3
  • 33
  • 68
23
votes
9 answers

Breaking a "for" loop using "break" considered harmful?

Some days ago I started a quick open source project and, when some mates looked at the code on svn, one of them told me that using break statement inside a for loop is considered harmful and shouldn't be done. He added, though, that I would find…
José Tomás Tocino
  • 9,873
  • 5
  • 44
  • 78
22
votes
17 answers

Continue Considered Harmful?

Should developers avoid using continue in C# or its equivalent in other languages to force the next iteration of a loop? Would arguments for or against overlap with arguments about Goto?
Brian
  • 5,826
  • 11
  • 60
  • 82
22
votes
9 answers

Other ways to deal with "loop initialization" in C#

To start with I'll say that I agree that goto statements are largely made irrelevant by higher level constructs in modern programming languages and shouldn't be used when a suitable substitute is available. I was re-reading an original edition of…
Peter Oehlert
  • 16,368
  • 6
  • 44
  • 48
22
votes
4 answers

Using Goto function across different functions

How can I use goto across different functions? For example: int main() { // .... REACH: // .... } void function() { goto REACH; }
Bhushanam Bhargav
  • 301
  • 1
  • 4
  • 10
22
votes
4 answers

effect of goto on C++ compiler optimization

What are the performance benefits or penalties of using goto with a modern C++ compiler? I am writing a C++ code generator and use of goto will make it easier to write. No one will touch the resulting C++ files so don't get all "goto is bad" on me.…
unixman83
  • 9,421
  • 10
  • 68
  • 102
21
votes
9 answers

To use goto or not?

This question may sound cliched, but I am in a situation here. I am trying to implement a finite state automaton to parse a certain string in C. As I started writing the code, I realised the code may be more readable if I used labels to mark the…
Abhinav Upadhyay
  • 2,477
  • 20
  • 32
21
votes
9 answers

goto in Java bytecode

So the other day when I was looking at the wikipedia page for Java bytecode I came across this example: Consider the following Java code: outer: for (int i = 2; i < 1000; i++) { for (int j = 2; j < i; j++) { if (i % j == 0) …
Levenal
  • 3,796
  • 3
  • 24
  • 29
21
votes
6 answers

Is using goto a legitimate way to break out of two loops?

I am solving problem 9 on the Project Euler. In my solution I use a "goto" statement to break out of two for loops. The Problem is the following: A Pythagorean triplet is a set of three natural numbers, a b c, for which, a^2 + b^2 = c^2 For…
Lucas
  • 13,679
  • 13
  • 62
  • 94
20
votes
3 answers

(Windows batch) Goto within if block behaves very strangely

If I take the following Windows batch code snippet and run it: echo foo if 1 == 1 ( echo bar goto asdf :asdf echo baz ) else ( echo quux ) The output I would expect is: foo bar baz But instead I get: foo bar baz quux If I comment…
coledot
  • 2,646
  • 3
  • 19
  • 17
20
votes
6 answers

Validity of forcing at least one iteration of for loop with goto statement

Disclaimer: I know it is obscure, and I would not program like that. I am aware of the preferred do-while statement, rather than that, the question is more about validity of a specific language construct. Is goto always supposed to omit conditional…
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137