State Machines, The Ultimate Goto

Richard Harter g-rh at cca.CCA.COM
Mon May 9 04:04:01 AEST 1988


In article <4627 at ihlpf.ATT.COM> nevin1 at ihlpf.UUCP (00704a-Liber,N.J.) writes:
>In article <27568 at cca.CCA.COM> g-rh at CCA.CCA.COM.UUCP (Richard Harter) writes:

>>Goto logic says leave and don't come back.

>Not true.  For example:

>	FOO:	goto FOO;

	You misapprehend.  This is not an example of 'goto' logic -- it is
an implementation of a simple loop using goto's.

>Goto logic says you can come here anytime you need to and from anywhere you
>want to (within limits, of course).

	Well, this is a matter of terminology -- what precisely does one mean
by the phrase 'goto logic', which is actually a neologism.  Since I coined
the phrase, I will claim priority and say it is my definitions that should
be used :-).  Seriously, however, your definition is not of much use, because
what you say is true of procedure calls also.  The difference is:

Procedure logic:  Record where you are now, transfer to a labelled block
(called a procedure) and transfer back to the recorded transfer point upon
completion of the block.

Goto logic:  Do not record where you are now; simply transfer to a labelled
block (no standard name).  Determine within the block the next block to be
executed.  The essence of 'goto logic' is that there is no return point to
return to.

	In theory, procedure logic is stronger than simple goto logic, i.e.
you can simulate an N statement program which uses gotos but not procedures
with an O(N) statement program using procedures but no gotos, said procedure
using program running in O(the execution time of the original program).
Conversely there are programs using procedures which cannot be replaced by
a program only using gotos without paying a penalty either in space or time.
[The reason for this is that the return is actually a 'computed goto' which
is stronger than the simple goto.]

>>The prescription against goto's really means -- don't mix
>>the two types of structure.

>I think I agree with you (I'll have to ponder this a little while longer).

	It is worth mentioning that the reason for the stricture is that
in 'goto logic' all control is at the same level.  In truth, most real
examples of goto logic are actually embedded goto logic, where there is
a remembered return point that any 'goto block' can exit to.  I.e. you
put a state machine inside a block and exit the state machine by exiting
the block.

	I have added comp.lang.misc to the group list and have directed
followups there, because this is no longer particularly a C topic.
-- 

In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die.
	Richard Harter, SMDS  Inc.



More information about the Comp.lang.c mailing list