ID:265767
 
Would anyone spare me an example of where use goto is legitimate? I know there are cases which it is legitimate in, but none come to mind.
DivineO'peanut wrote:
Would anyone spare me an example of where use <code>goto</code> is legitimate? I know there are cases which it is legitimate in, but none come to mind.

Those are very few and far in between.
Error handling where the language lacks in such ways. Breaking out of heavily nested loops.
In response to Audeuro
Audeuro wrote:
Error handling where the language lacks in such ways.

Such as?

Breaking out of heavily nested loops.

Why not just use break? =)

-- Data
In response to Audeuro
I know how it's supposed to be used, but I can't seem to think of a situation where I'd actually use it. What I am more interested in is code where it is used in, rather than being told where it "should" be used.
In response to Android Data
Android Data wrote:
Audeuro wrote:
Error handling where the language lacks in such ways.

Such as?

C. It lacks structured exceptions, so one would have to 'goto' the end of the function.

Breaking out of heavily nested loops.

Why not just use break? =)

-- Data
In response to DivineO'peanut
If you already know where it should be used, then why does this thread even exist? You should be able to identify a situation where you'd need it if you already know when it should be used.
In response to Audeuro
I know how it's used on the "Error handling where the language lacks in such ways. Breaking out of heavily nested loops." border, but I also know that I could just <code>break</code> or <code>return</code> in these cases, and I couldn't think of a situation where <code>goto</code> would be preferable.

So will you(or anyone that can) please answer my question?
In response to DivineO'peanut
Let's say I have three loops, a loop in a loop in a loop, and a condition in the inner-most loop needs to completely break out of the loop, and continue on with execution like normally. You /could/ make another function for the stuff afterward, but that's ugly, since this condition may exist in some situations, but not others.

Let's look at some pseudo-code for another situation.

for i = arg_1 to arg_2 * 2
for j = arg_2 to arg_2 + 6
for k = arg_3 to arg_3 * 5
if(k == (j * 2)) goto out_i;
k += 2;
j ++;
i += 3;

out_i:
arg_1 += 5;
arg_2 -= 10;
arg_3 ++;

return;


That's not the best example, but I can't really demonstrate what I mean without getting too deep into it. Anyhow- I have three loops nested within each other, each doing their own thing. The problem is that if a certain condition is met with the inner-most, I need to get out of it and continue on with my procedure, which I can't do with a simple break /or/ return. A new procedure isn't going ot cut it, because whatever's after out_i might rely on certain arguments to be present, or you might change the arguments later on, and you'll need to pass those and modify the new procedure just to fit that, which just isn't really feasible, since you may end up modifying tiny things in this one little procedure so many times.

Now, to expand on our above pseudo-code, here's an example where goto really isn't acceptable:

for i = arg_1 to arg_2 * 2
i += 3;
if(i == 6) goto out_i;

out_i:
arg_1 += 5;
arg_2 -= 10;

return;


There, you /could/ break out and easily do what you need to do, and you really don't need that extra jump in there.




Exception handling is a different case. Sometimes, you may want to output a message or something in a consistent manner. In C++, you can use try...catch...throw. Well, C (old-style C, at least) lacked in this area greatly, so if you ran into an error, you'd end up having to set the message up, then go to the handler.

Example:

let debug_msg = "";
let line = -1;

if(denominator == 0)
line = 6
debug_msg = "Cannot divide by zero."
goto exception_handler;

return (numerator / denominator)

exception_handler:
output("ERROR (Line " + line + "): " + debug_msg);
return -1;


In response to Audeuro
Audeuro wrote:
Let's say I have three loops, a loop in a loop in a loop, and a condition in the inner-most loop needs to completely break out of the loop, and continue on with execution like normally. You /could/ make another function for the stuff afterward, but that's ugly, since this condition may exist in some situations, but not others.

main_loop:
for i = arg_1 to arg_2 * 2
for j = arg_2 to arg_2 + 6
for k = arg_3 to arg_3 * 5
if(k == (j * 2)) break main_loop;
k += 2;
j ++;
i += 3;
arg_1 += 5;
arg_2 -= 10;
arg_3 ++;

return;


-- Data
In response to Android Data
He never specified DM. In most other languages, that feature isn't available.
In response to Audeuro
Right. Thanks for clearing that up for me! I feel stupid for not thinking of such a simple thing. =)
In response to DivineO'peanut
DivineO'peanut wrote:
Right. Thanks for clearing that up for me! I feel stupid for not thinking of such a simple thing. =)

Sorry to bust into this topic, but...
Whats your MSN or AIM Divine? I lost it. :(
In response to Flame Sage
Flame Sage wrote:
DivineO'peanut wrote:
Right. Thanks for clearing that up for me! I feel stupid for not thinking of such a simple thing. =)

Sorry to bust into this topic, but...
Whats your MSN or AIM Divine? I lost it. :(

There is a pager for a certain reason, and you're a BYONDmember so you really have no excuse.
I used it several times in Regex, in some heavily nested loops. In one case it actually serves to execute code that's used for several common cases, where a proc would simply not be appropriate.

Lummox JR
In response to Lummox JR
You know a bunch about programming--I've heard that using goto can cause problems with memory allocation and/or garbage collection, such as variable x in nested loop still occupying memory space after you've goto'd out of it. Is this true?
In response to CaptFalcon33035
CaptFalcon33035 wrote:
You know a bunch about programming--I've heard that using goto can cause problems with memory allocation and/or garbage collection, such as variable x in nested loop still occupying memory space after you've goto'd out of it. Is this true?

It depends on the language. In the case of BYOND it's just gonna free up anything that was in the loop, at least when the proc ends if not when you goto out. Still though, to avoid that trouble just don't create new vars inside the nested loops.

Internally, all compilers reduce loop instructions to jumps. Java even has a goto in its bytecode, yet the language doesn't technically allow goto. BYOND uses jump instructions for its loops as well.

Lummox JR