break, continue, return, goto

Frank Adams franka at mmintl.UUCP
Wed Dec 4 04:08:28 AEST 1985


[Not food]

The real case for imbedded returns is in the following sort of construct:

if (a) {
   stuff;
   if (b) {
      more stuff;
      if (c) {
         return;    /* oops! */
      }
      still more stuff;
   }
   even more stuff;
}

One can deal with "still more stuff" with an 'else if' clause, but "even
more stuff" is harder.  Basically, one needs a switch:

bool ok;
if (a) {
   ok = TRUE;
   stuff;
   if (b) {
      more stuff;
      if (c) {
         ok = FALSE;    /* oops! */
      }
      else {
         still more stuff;
      }
   }
   if (ok) {
      even more stuff;
   }
}

Personally, I think this is distincly less readable than the first version.
Furthermore, as the complexity of the program flow increases, the amount of
overhead to keep track of switches increases; the cost of a return or break
does not.

There does come a point where it is better to split the parts into separate
modules, but I think that point is at about twice the complexity of my
example.  This leaves lots of intermediate programs where return or break
is a clear win.  (Raising exceptions is better, but isn't available in c.)

Frank Adams                           ihpn4!philabs!pwa-b!mmintl!franka
Multimate International    52 Oakland Ave North    E. Hartford, CT 06108



More information about the Comp.lang.c mailing list