1)can any one give an example for a C program that can be written
using goto only ? i.e a program in which we cannot avoid the keyword
goto
Perhaps it is worthwhile going into this a bit deeper since most
people haven't read the actual papers. I have and have them
filed away somewhere - where is another matter. Briefly, and to
the best of memory, there is a heirarchy of flow control
structures which runs something like this:
while loop
infinite loop with break
goto fixed target
function call/return
goto selectable target
The first major result was the Boehm-Jacopini theorem that showed
that all other control structures can be translated into while
loops. There is, however, a catch. These translations require
extra code and/or extra time. There are tradeoffs; you can trade
space for time. There are "proof of concept" programs for which
the growth in required space and time is very bad, e.g.,
polynomial growth in time or exponential growth in space as you
increase the program size.
The list is in order of the power of the construct. That is, the
"goto selectable target" construct aka the computed goto or the
switch is the most powerful construct. There are programs that
can be programmed efficiently using switches that cannot
otherwise be programmed efficiently. (These tend to be very
peculiar programs.)
Structured programming uses the while loop, function call/return,
and the switch (which actually is slightly weaker than the
full-blown selectable target goto) with the infinite loop being
optional.
Perhaps the reason that the goto controversey never quite dies is
that in structured programming within the actual body of
functions the constructs that are used (sans switches) are weaker
than the goto. The effect of this is that in some places there
are unavoidable little awkwardnesses. However this doesn't
amount to much because (good) programs are broken into relatively
short functions. This has two good effects. Firstly, since
functions are short the inefficiencies of the weaker constructs
do not accumulate. Secondly, the cumbersome goto is replaced by a
more powerful construct.
IHTH, HAND.
Richard Harter, (e-mail address removed)
http://home.tiac.net/~cri,
http://www.varinoma.com
In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die