gotos
Gary M. Samuelson
garys at bunker.UUCP
Tue Apr 26 08:31:26 AEST 1988
In article <2200 at louie.udel.EDU> new at 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
More information about the Comp.lang.c
mailing list