Put your code... (was Re: gotos

Richard Harter g-rh at cca.CCA.COM
Fri Apr 29 01:58:52 AEST 1988


In article <4700011 at uiucdcsm> wsmith at uiucdcsm.cs.uiuc.edu writes:

>This is an alternative technique that may avoid the goto:

>	procedure () {
>		prolog code
>		do {
>			main body
>		} while (0);
>		epilog code
>		}


>A break or continue inside the 1 time only do-while will jump
>to the epilog code.  I think this is only an academic curiosity, and
>I haven't ever seen code actually using this construct.  Has anyone
>actually written code with this in it?  The optimizer should generate
>the same code as if goto's were used directly.

	Yep.  Once in a great while I use that particular technique.
Although the language purists may frown, I have stashed away in the
standard include file the following defines:

#define BLOCK do {
#define ENDBLOCK } while(0);

#define LOOP for(;;) {
#define ENDLOOP }

The BLOCK/ENDBLOCK combo is mostly useful in situations like this

	LOOP
	  get_next_thingy
	  do_some_stuff
	  BLOCK
	    if (Special_case_1) {
              handle special case;
	      break;
              }
	    intervening code;
            if (special case 2) {
              take care of it;
              break;
              }
            more intervening code;
            ....
            ENDBLOCK
          more stuff
	  ENDLOOP

You can, of course, handle this with nested if's, but I for one think that
the resulting code is kludgy, e.g.

	if (special case 1) {
          take care of it; break; }
        else {
          intervening code;
          if (special case 2) {
            take care of it; break; }
          else {
          ....

The problem with this code is that it obscures the essential structure of
what is being done  --  the task is to filter the thingy though a series of
tests and quit testing when a test is passed.  And now for my little 
grasshopper for programmers:

"The essence of structured programming is to identify the essential structure
of the problem and arrange the code to reflect that structure."


-- 

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