Usage of goto's

Ken Lerman lerman at stpstn.UUCP
Sat Sep 29 23:01:41 AEST 1990


In article <1990Sep28.121230.17767 at NCoast.ORG> ramsey at NCoast.ORG (Cedric Ramsey) writes:
->
->Hello once again peoplekind. I have a question about goto's. We all
->know that there usage is taboo in any language but what better way 
->do we have to implement finite state machines. A switch statement ?
->All a switch statement is a goto statement, internally that is. So
->then, maybe it is okay to implement goto's for NFA's, DFA's, and
->transition diagrams.
->
->Is it okay for a programmer to break a TABOO intentionally; by design?

IMHO, yes.  In one situation where I cared about efficiency, I felt
justified in using a bunch of gotos in the same program.  The code looked
something like this.

/*
    state1 is the state where some pixels may be clipped.
    state2 is the state where all pixels are clipped.
    state3 is the state where no pixels are clipped.
*/

state1name:
{
   /*...*/
   if(someCondition)goto state1name;
   else if(someOtherCondition)goto state2name;
   else goto state3name;
}

state2name:
{
    /*...*/
    if(someCondition)goto state3name;
    else goto state1name;
}

state3name:
{
    /*...*/
    if(someCondition)goto state2name;
    else goto state1name;
}

Each state was carefully commented as to its functionality.  Each was
a block of code which was entered at the top.  At the time, I felt
that the code could be written most cleanly using gotos.  (That may
not be true of the code above.)

Of course, the other place one should feel free to use gotos is as the
output of an automatic tool (such as YACC).

Ken



More information about the Comp.lang.c mailing list