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
76
votes
5 answers

How to break out of multiple loops at once in C#?

What if I have nested loops, and I want to break out of all of them at once? while (true) { // ... while (shouldCont) { // ... while (shouldGo) { // ... if (timeToStop) { break; //…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
74
votes
14 answers

Is it possible to store the address of a label in a variable and use goto to jump to it?

I know everyone hates gotos. In my code, for reasons I have considered and am comfortable with, they provide an effective solution (ie I'm not looking for "don't do that" as an answer, I understand your reservations, and understand why I am using…
Joshua Cheek
  • 30,436
  • 16
  • 74
  • 83
67
votes
8 answers

Is using a 'goto' statement bad?

After doing some reseach on how to break through a secondary loop while (true) { // Main Loop for (int I = 0; I < 15; I++) { // Secondary loop // Do Something break; // Break main loop? } } most people recommended to call the…
Rasmus Søborg
  • 3,597
  • 4
  • 29
  • 46
65
votes
35 answers

To GOTO or not to GOTO?

Currently I am working on a project where goto statements are heavely used. The main purpose of goto statements is to have one cleanup section in a routine rather than multiple return statements. Like below: BOOL foo() { BOOL bRetVal = FALSE; …
anand
  • 11,071
  • 28
  • 101
  • 159
64
votes
10 answers

Being pressured to GOTO the dark-side

We have a situation at work where developers working on a legacy (core) system are being pressured into using GOTO statements when adding new features into existing code that is already infected with spaghetti code. Now, I understand there may be…
Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
59
votes
7 answers

Use a 'goto' in a switch?

I've seen a suggested coding standard that reads Never use goto unless in a switch statement fall-through. I don't follow. What exactly would this 'exception' case look like, that justifies a goto?
Brent Arias
  • 29,277
  • 40
  • 133
  • 234
56
votes
3 answers

Do any compilers for the JVM use the "wide" goto?

I figure most of you know that goto is a reserved keyword in the Java language but is not actually used. And you probably also know that goto is a Java Virtual Machine (JVM) opcode. I reckon all the sophisticated control flow structures of Java,…
Alonso del Arte
  • 1,005
  • 1
  • 8
  • 19
56
votes
3 answers

Why do some kernel programmers use goto instead of simple while loops?

When I learned C, teacher told me all day long: "Don't use goto, that's a bad habit, it's ugly, it's dangerous!" and so on. Why then, do some kernel programmers use goto, for example in this function, where it could be replaced with a simple…
musicmatze
  • 4,124
  • 7
  • 33
  • 48
54
votes
5 answers

Goto out of a block: do destructors get called?

Consider the following code: void foo() { { CSomeClass bar; // Some code here... goto label; // and here... } label: // and here... } Will the destructor of bar be called ?
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
54
votes
6 answers

The equivalent of a GOTO in python

I am self teaching myself python 2.7. I have some experience in using BATCH, which has a GOTO statement. How do I do that in python? For example, suppose I want to jump from line 5 to line 18. I realize there have been previous questions regarding…
Calder Hutchins
  • 755
  • 1
  • 6
  • 10
50
votes
3 answers

What does a && operator do when there is no left side in C?

I saw a program in C that had code like the following: static void *arr[1] = {&& varOne,&& varTwo,&& varThree}; varOne: printf("One") ; varTwo: printf("Two") ; varThree: printf("Three") ; I am confused about what the && does because there is…
Joshua
  • 547
  • 4
  • 6
41
votes
2 answers

Ill-formed goto jump in C++ with compile-time known-to-be-false condition: is it actually illegal?

I am learning about some dark corners of C++ and, in particular, about the "forbidden" goto and some restrictions on its usage. This question is partially inspired by Patrice Roy's talk at CppCon 2019 "Some Programming Myths Revisited" (link to…
Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
37
votes
8 answers

When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto (in C)?

When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto? Thanks, Chenz
Crazy Chenz
  • 12,650
  • 12
  • 50
  • 62
36
votes
2 answers

Is it possible to use goto with switch?

It seems it's possible with C#, but I need that with C++ and preferably cross platform. Basically, I have a switch that sorts stuff on single criteria, and falls back to default processing on everything else. Say: switch(color) { case GREEN: case…
Coder
  • 3,695
  • 7
  • 27
  • 42
34
votes
4 answers

How do I get GDB to break out of a loop?

I can tell GDB to return from a function immediately with return, and call a function with call myFunction. But how do I get it break out of the current loop? i.e. to act as if it's hit a break; statement. Is jump myfile.c: the way to…
John Carter
  • 53,924
  • 26
  • 111
  • 144
1
2
3
70 71