goto's in 'C'

Thomas M. Breuel breuel at harvard.ARPA
Fri Jan 25 01:21:49 AEST 1985


|From rpw3 at redwood.UUCP (Rob Warnock) Tue Jan 22 21:17:38 1985
|
|+---------------
|| >[rpw3] The incidence of the "goto" in C code is so rare anyway, I dare say
|| >we could abolish it altogether (replacing it with BLISS's "leave")
|| >and not miss the loss. (I certainly never missed it in BLISS.)
|| I would miss it. I have never used it for breaking out of loops or
|| making loops in the conventional sense. It is extremely useful, though,
|| for 'hardcoding' finite state machines. If they contain more than say 3
|| or 4 states, the use of structured constructs is awkward...
|| 						Thomas.
|+---------------
|
|Well, that depends on what "structured construct" you use. The standard
|technique (criticized by Wulf in "Global Variable Considered Harmful") is
|to use a (global) state variable which is really the "PC" of your state
|machine, with assignments to "state" instead of "goto"s:

I would not refer to a state machine with an explicit state variable
as 'hard coded' (sorry about the fuzzy terminology). It remains that in
many cases, state machines implemented with 'goto' are at least as
readable as those implemented using a state variable and a while loop. In
addition, they don't require you to define your states in a separate
place and they are more efficient.

'goto' is useful when used with care, and not providing it in a
systems programming language like 'C' would probably be a mistake.

						Thomas.



More information about the Comp.lang.c mailing list