Xref: utzoo comp.lang.c:9796 comp.software-eng:501 Path: utzoo!mnetor!uunet!mcvax!ukc!stl!stc!datlog!dlhpedg!cl From: cl@datlog.co.uk (Charles Lambert) Newsgroups: comp.lang.c,comp.software-eng Subject: Re: gotos Message-ID: <760@dlhpedg.co.uk> Date: 28 Apr 88 12:51:30 GMT References: <1988Apr8.183815.3187@utzoo.uucp> <449@goofy.megatest.UUCP> <2200@louie.udel.EDU> <587@vsi.UUCP> <27310@cca.CCA.COM> Sender: news@dlhpedg.co.uk Reply-To: cl@datlog.co.uk (Charles Lambert) Organization: FSD@Data Logic Ltd, Queens House, Greenhill Way, Harrow, London. Lines: 50 In article <27310@cca.CCA.COM> g-rh@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