Re: acceptable use of goto?



christian.bau wrote:
On Mar 20, 8:18 pm, Flash Gordon <s...@xxxxxxxxxxxxxxxxxx> wrote:

Wait until you have worked on code that branches forward in to a
loop, forward within the loop at various points, then forward out of
the loop. Then tell me that your rule is sufficient. BTW, I'm
referring to real code I've had to maintain, and it is horrible.

Some time thirty years ago it was proved that any program that uses
goto's can be rewritten without gotos. The proof is quite simple:
First, you replace all structured statements with simple statements
and gotos, so you end up with only statements of the form "expression-
statement", "goto", "if (expression) goto", with a return statement at
the end. Then you give every statement a label. Create a variable
named "label" and initialise it to the label of the first statement.
Then the rest is translated to

"while (label != <label of the return statement) {
if (label == <label of statement 1>} { statement1; label =
<label of statement2>; } // example of expression statement
else if (label == <label of statement2>} { label = <label of
goto destination>; } // example of goto statement
else if (label == <label of statement3>) { label = (expr) ?
<label of goto destination> : <label of statement4>; } // Example of
conditional goto
;;;
}

No goto statements. No structure either.

If I understood this correctly: I take my beautifully structured program,
and decompose it into simple statements and gotos.

Then I build a framework (of if or switch statements) to eliminate the
gotos?

A lot of work to eliminate the occasional goto as in the following
(uncompiled) code:

for (i=1; i<=N; +=i)
{
if (x==2) break;

switch (x)
{
case 2:
goto mylabel;
};

};
mylabel:

I want to break out of the loop when x=2, which I can do with an 'if'
statement but not with a 'switch' statement, because the latter uses the
same 'break' keyword as used for loops. I can do this with extra code but
this would generate more clutter than a goto would.

And in fact there are many other well-structured uses of goto to eliminate a
few other deficiences in the C syntax, like breaking out of a nested loop;
this is a similar problem to the above, I can't break out of a loop using
break, from inside an nested loop.

I think 'goto' used according to certain rules and situations can be
perfectly acceptable.

The above goto elimination scheme sounds useful however when port a language
with gotos, or with different control structures, to one without.

--
Bart



.



Relevant Pages

  • Re: address of a statement in C
    ... Not until you've accounted for every goto and traced ... >> every possible execution path. ... Not until you've accounted for every if and loop and traced every ... in absence of a label, you *know* that I will be initialized to 1 ...
    (comp.lang.c)
  • Re: Pse, help me to understand this code
    ... hidden it inside a bogus loop construct. ... If you _must_ use goto (and sometimes you must, ... The problem with the goto approach is that, on seeing the label, you ... If ESCAPE was called when you were not in a TRY RECOVER block it acted ...
    (comp.lang.c)
  • make buildkernel KERNCONF=CUSTOM errors!!!
    ... case label not within a switch statement ... break statement not within loop or switch ...
    (freebsd-questions)
  • Re: what dialect of fortran is this ?
    ... >Ok to help you all out the original compiler was some version of LAHEY ... >do loop etc ... all the labels in the assigned goto being present. ... the jump to the label given in the call is made. ...
    (comp.lang.fortran)
  • Re: How to "continue" in nested do-while-loop?
    ... With goto this should be no problem in while-loops. ... //continue outer loop ... Placing it at A gives me a "label at end of compound statement", ...
    (comp.lang.c)

Loading