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
33
votes
14 answers

How to avoid use of goto and break nested loops efficiently

I'd say that it's a fact that using goto is considered a bad practice when it comes to programming in C/C++. However, given the following code for (i = 0; i < N; ++i) { for (j = 0; j < N; j++) { for (k = 0; k < N; ++k) { …
rual93
  • 553
  • 4
  • 11
33
votes
8 answers

How to use goto statement correctly

I am taking my high school AP Computer Science class. I decided to throw a goto statement into a one of our labs just to play around, but I got this error. Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax…
Ungeheuer
  • 1,393
  • 3
  • 17
  • 32
33
votes
2 answers

Statement goto can not cross variable definition?

Suppose these code compiled in g++: #include int main() { int a =0; goto exit; int *b = NULL; exit: return 0; } g++ will throw errors: goto_test.c:10:1: error: jump to label ‘exit’ [-fpermissive] goto_test.c:6:10:…
coanor
  • 3,746
  • 4
  • 50
  • 67
32
votes
20 answers

Is there ever a reason to use goto in modern .NET code?

I just found this code in reflector in the .NET base libraries... if (this._PasswordStrengthRegularExpression != null) { this._PasswordStrengthRegularExpression = this._PasswordStrengthRegularExpression.Trim(); if…
BenAlabaster
  • 39,070
  • 21
  • 110
  • 151
30
votes
4 answers

Address of labels (MSVC)

We are writing a byte-code for a high-level compiled language, and after a bit of profiling and optimization, it became clear that the current largest performance overhead is the switch statement we're using to jump to the byte-code cases. We…
Trevor Sundberg
  • 1,584
  • 1
  • 14
  • 25
30
votes
1 answer

Labels - break vs continue vs goto

I understand that: break - stops further execution of a loop construct. continue - skips the rest of the loop body and starts the next iteration. But how do these statements differ when used in combination with labels? In other words what is the…
Dcompoze
  • 853
  • 1
  • 11
  • 19
29
votes
10 answers

VB.NET Switch Statement GoTo Case

I am writing some code in VB.NET that uses a switch statement but in one of the cases it needs to jump to another block. In C# it would look like this: switch (parameter) { case "userID": // does something here. case "packageID": …
KTF
  • 1,349
  • 2
  • 14
  • 21
29
votes
6 answers

`goto` in Python

I must use goto in Python. I found entrians goto but my Python implementation (CPython 2.7.1 on Mac) does not have this module, so it doesn't seem to be portable. It should at least work in all Python implementations which support CPython bytecode…
Albert
  • 65,406
  • 61
  • 242
  • 386
28
votes
13 answers

Design pattern that can replace chained switch/goto?

I have a code for updating my application resources to current application version. This code is called after application update. int version = 1002; // current app version switch(version) { case 1001: updateTo1002(); goto case…
Przemysław Michalski
  • 9,627
  • 7
  • 31
  • 37
26
votes
16 answers

Should I avoid using goto here? If so, how?

I am coding for a function that takes a hand and checks for pairs: int containsPairs(vector hand) { int pairs{ 0 }; loopstart: for (int i = 0; i < hand.size(); i++) { Card c1 = hand[i]; for (int j = i + 1; j <…
Alex
  • 620
  • 1
  • 6
  • 20
26
votes
2 answers

Why disallow goto in constexpr functions?

C++14 has rules for what you can and can't do in a constexpr function. Some of them (no asm, no static variables) seem pretty reasonable. But the Standard also disallows goto in constexpr functions, even while it allows other control flow…
Sneftel
  • 40,271
  • 12
  • 71
  • 104
26
votes
7 answers

Elegant way for exiting a function neatly without using goto in C

We often write some functions which have more than one exit point (that is, return in C). At the same time, when exiting the function, for some general works such as resource cleanup, we wish to implement them only once, rather than implementing…
ACcreator
  • 1,362
  • 1
  • 12
  • 29
26
votes
3 answers

GOTO before local variable

Does the following piece of code constitute undefined behaviour, since I am jumping before the variable declaration and using it via a pointer? If so, are there differences between the standards? int main() { int *p = 0; label1: if (p) { …
Pascal Kesseli
  • 1,620
  • 1
  • 21
  • 37
25
votes
9 answers

Get out of multiple loops?

Possible Duplicate: Breaking out of a nested loop I have this code foreach (___) { foreach (___) { foreach (___) { if (condition) { //break out of all loops } } …
user1153455
25
votes
3 answers

C : stack memory, goto and "jump into scope of identifier with variably modified type",

I found that this refuses to compile : int test_alloc_stack(int size){ if(0) goto error; // same issue whatever conditional is used int apply[size]; give_values(apply,size); return 1; error: return 0; } The error I get…
Vince
  • 3,979
  • 10
  • 41
  • 69
1 2
3
70 71