gotos
Charles Lambert
cl at datlog.co.uk
Thu Apr 28 22:51:30 AEST 1988
In article <27310 at cca.CCA.COM> g-rh at CCA.CCA.COM.UUCP (Richard Harter) writes:
>
>f) Put the test condition in a while statement and step through the
> actions [somehow]. Unfortunately, I don't see a nice way to do
> this in C. The thought that first occurs to one is something like
>
>for (step=0,done=0;!done;step++) {
> switch(step) {
> case 0: action_0;
> break;
> case 1: action_1;
> break;
> ......
> default: done = 1;
> break;
> }
> if (!done && test_condition) done=1;
> }
This is almost a finite-state machine, and state machine programming is an
accepted idiom. Generalise it as follows:
state=STAT0;
while ( state != STATE_DONE ) {
switch (state) {
STATE0: action_0();
if (exit_condition_0)
state = STATE_DONE;
else
state=STATE1;
break;
STATE1: action_1();
if (exit_condition_1)
state = STATE_DONE;
else
state = STATE2;
break;
.
.
.
}
}
Note that with this model, states may occur in any order and even
repetitively. The "if...goto" construct is simply a sequential case of the
general state machine.
-----------------
Charles Lambert
More information about the Comp.lang.c
mailing list