Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!tut.cis.ohio-state.edu!cs.utexas.edu!romp!auschs!awdprime!commlab1!tif From: tif@commlab1..austin.ibm.com (/32767) Newsgroups: comp.lang.c Subject: Re: Goto or not goto? (Was: Re: loops in general) Keywords: goto state-machine Message-ID: <2296@awdprime.UUCP> Date: 4 May 90 18:20:15 GMT References: <1565@amethyst.math.arizona.edu> <682@bbxsda.UUCP> <2106@l.cc.purdue.edu> <12866@ulysses.att.com> Sender: jroot@awdprime.UUCP Reply-To: cs.utexas.edu!auschs!ibmaus!commlab1.austin.ibm.com!tif Organization: IBM AWD, Austin, TX Lines: 54 In article <12866@ulysses.att.com> ggs@ulysses.att.com (Griff Smith) writes: >How do you plan to debug your state machine? If you have it filled >with gotos, you have to follow each state transition the hard way. If >you use a switch on a state variable, you get to add code between the >`for (;;)' and the switch to log state transitions, look for strange >transitions, etc. I think you're doing a disservice to mortals to >encourage them to use idioms that are only manageable by higher forms of >life. Consider the following: #ifdef DEBUG #define STATE(x) case x #define TO_STATE(x) {state=x;break;} int state; for(;;) { printf("Going to state %d\n", state); switch(state) { #else #define STATE(x) lbl##x #define TO_STATE(x) goto lbl##x #endif STATE(0): if (...) { ... TO_STATE(0); } else TO_STATE(5); STATE(5): if (blah) TO_STATE(0); ... #ifdef DEBUG } } #endif I haven't tried compiling it and I don't know if I used the ## operator correctly but I think you get the drift. Now I put to you the question, does this code suffer from the minuses of "goto" or have the advantages of the "switch"? I'm not saying I would write the code like this but I think I'll let all of you think about how the previously stated pros and cons apply to this code fragment. (BTW, break won't work right easily inside nested control structures.) Paul Chamberlain tif@doorstop.austin.ibm.com tif@commlab1.austin.ibm.com sc30661@ausvm6