Path: utzoo!mnetor!uunet!lll-winken!lll-tis!ames!oliveb!bunker!garys From: garys@bunker.UUCP (Gary M. Samuelson) Newsgroups: comp.lang.c Subject: Re: gotos Message-ID: <3490@bunker.UUCP> Date: 25 Apr 88 22:31:26 GMT References: <1988Apr8.183815.3187@utzoo.uucp> <449@goofy.megatest.UUCP> <1988Apr11.201934.20594@utzoo.uucp> <748@l.cc.purdue.edu> <3470@bunker.UUCP> <2200@louie.udel.EDU> Reply-To: garys@bunker.UUCP (Gary M. Samuelson) Organization: Bunker Ramo, an Olivetti Company, Shelton, Ct Lines: 60 In article <2200@louie.udel.EDU> new@udel.EDU (Darren New) writes: >How about: (pseudocode) For pseudocode it looks a lot like C. The easy way: replace "goto handle_key" with "return." Then after the call to this function, add a call to handle_key(). You have too much going on for it to be one function anyway. What you really want is to get an interrupt if a key is pressed. If you can do that, then the code becomes much simpler. If you can't get either a "key pressed" interrupt or an interval timer interrupt, then use a state machine. (Credit has to go to Dave Burton; although I frequently use state machines, I hadn't thought of it in the previous case.) while( state != done ) { /* Instead of 'state', 'next_thing_to_do' might be better */ switch( state ) { case readingfile: ... if( read_line[0] == special_flag_char ) state = done; else state = inserting_spaces_and_color_codes; break; case inserting_spaces_and_color_codes: ... state = displaying_text_line; break; ... case last_thing_to_do: state = done; break; } /* end of switch (state) */ if( keypressed() ) state = done; } /* end of while( state != done ) */ c = wait_for_key_then_read_it(); switch (c) { case 'A': ... ... } It still reads sequentially, but now "if keypressed" is isolated, which makes it less cluttered, and easier to change if (no, when) it becomes necessary to do any of the following: 1. pass an argument to keypressed(); 2. check something else periodically; 3. add or delete points at which the check is made; >I would like to see a goto-less version of this that is easier to understand. I hope you think I have done so. Gary Samuelson