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
19
votes
10 answers

C/C++: is GOTO faster than WHILE and FOR?

I know, that everybody hates GOTO and nobody recommends it. But that's not the point. I just want to know, which code is the fastest: the goto loop int i=3; loop: printf("something"); if(--i) goto loop; the while loop int i=3; while(i--) { …
Artur Iwan
  • 1,151
  • 2
  • 10
  • 17
19
votes
1 answer

Are goto and destructors compatible?

This code leads to undefined behavior: void some_func() { goto undefined; { T x = T(); undefined: } } The constructor is not called. But what about this code? Will the destructor of x be called? I think it will be, but I want to be…
Sergey Skoblikov
  • 5,811
  • 6
  • 40
  • 49
19
votes
13 answers

while(1) .. break instead of goto

I found the following code in a C program: while (1) { do_something(); if (was_an_error()) break; do_something_else(); if (was_an_error()) break; [...] break; } [cleanup code] Here while(1) is used as local emulation…
dmeister
  • 34,704
  • 19
  • 73
  • 95
18
votes
7 answers

Is there a Java bytecode optimizer that removes useless gotos?

Problem: I have a method that compiles to over 8000 bytes of Java bytecode. HotSpot has a magic limit that makes the JIT not kick in for methods that exceed 8000 bytes. (Yes, it is reasonable to have a huge method. This is a tokenizer loop.) The…
hsivonen
  • 7,908
  • 1
  • 30
  • 35
18
votes
16 answers

PHP and the goto statement to be added in PHP 5.3

The "goto" statement comes straight out of ASM or any other assembler language. Here's a link: http://be2.php.net/manual/en/control-structures.goto.php I'm wondering: what can this do to make my code more well-organized? How can I implement this in…
KdgDev
  • 14,299
  • 46
  • 120
  • 156
18
votes
2 answers

What happens if you transfer control to a if(false) block by using goto?

I've thought of following code by trying to solve a difficult 'nested-condition' problem: goto error; if (false) { error: cout << "error block" << endl; } else { cout << "else block" << endl; } When I run this code, only error block is…
Be Ku
  • 319
  • 1
  • 6
18
votes
5 answers

Goto prior to a variable definition - what happens with its value?

Here is some question I wondered about. Given the following code, can we be certain about its output? void f() { int i = 0; z: if(i == 1) goto x; else goto u; int a; x: if(a == 10) goto y; u: a = 10; i = 1; goto z; y: std::cout <<…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
18
votes
11 answers

Do Perl loop labels count as a GOTO?

Generally, it is good practice to avoid GOTOs. Keeping that in mind I've been having a debate with a coworker over this topic. Consider the following code: Line: while( <> ) { next Line if (insert logic); } Does using a loop label…
Kavet Kerek
  • 1,305
  • 9
  • 24
18
votes
5 answers

Is there goto statement in Ruby?

Is there a way to start at a specified line, like a goto statement?
gpwu
  • 6,957
  • 3
  • 17
  • 9
18
votes
8 answers

Why does this "finally" execute?

If you run the code below it actually executes the finally after every call to the goto: int i = 0; Found: i++; try { throw new Exception(); } catch (Exception) { goto Found; } finally { …
Kredns
  • 36,461
  • 52
  • 152
  • 203
17
votes
2 answers

Is jumping over a variable initialization ill-formed or does it cause undefined behaviour?

Consider this code: void foo() { goto bar; int x = 0; bar: ; } GCC and Clang reject it, because the jump to bar: bypasses variable initialization. MSVC doesn't complain at all (except using x after bar: causes a warning). We can do a…
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
17
votes
3 answers

Batch - What is the difference between CALL and GOTO?

I understand both link to labels in the code, but what is the difference? @echo off :top echo I love StackOverflow.com goto :top @echo off :top echo I love StackOverflow.com call :top Thank you in advance!
Crazy Redd
  • 435
  • 1
  • 5
  • 18
17
votes
3 answers

What happens when we combine RAII and GOTO?

I'm wondering, for no other purpose than pure curiosity (because no one SHOULD EVER write code like this!) about how the behavior of RAII meshes with the use of goto (lovely idea isn't it). class Two { public: ~Two() { printf("2,"); …
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
16
votes
15 answers

Go To Statement Considered Harmful?

If the statement above is correct, then why when I use reflector on .Net BCL I see it is used a lot? EDIT: let me rephrase: are all the GO-TO's I see in reflector written by humans or compilers?
Yaron Naveh
  • 23,560
  • 32
  • 103
  • 158
16
votes
3 answers

Automated GOTO removal algorithm

I've heard that it's been proven theoretically possible to express any control flow in a Turing-complete language using only structured programming constructs, (conditionals, loops and loop-breaks, and subroutine calls,) without any arbitrary GOTO…
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477