break, continue, return, goto

cjl at iuvax.UUCP cjl at iuvax.UUCP
Mon Nov 11 04:40:00 AEST 1985



Craig made good suggestions for not using "continue" and "break"
especially for the examples of Michael. "Continue" goto the beginning
of the loop. Replacing it with "else" would make the logic flow of the
codes following the "break" more smoothly.
 
  "Break" is generally interpreted as an exception where logic flow 
is disrupted.  But for the searching problem, both found and not found
are normal results. We can hardly interpret "not found" as an undesired
result. In general exception, like error in system call,  will lead to 
program abnormal termination. Error recovery is hard to achieve, because
logic flow is disrupted.  Too many times, programmers are seduced by 
the use of break for a quick solution without spending time to structure 
their program more. That is the same lesson we learned from advocating 
goto-less programs.

  In addition, I recommend the use of for loop be restricted to 
its original meaning in natural language, i.e. as a loop with simple 
counter. With more complicate exit conditions, while loop fits better :

 	m = meeble;
 	while ((m < meeble+NMEEBLE) && (m->glop==forp || m->zip==fweep)){
          /* walk thru the array till we hit the end or
           * find the right one */
           ++m;
        };
 	if (m < meeble + NMEEBLE) {
         /* found it */
           printf("zeegle %2s", m->yorg);
           m->blazzo += IGUAP;
 	} else {
         /* not found it */
        }

In some language like Pascal there is no conditional boolean operator, 
we have to use boolean flages. With the availability of && and || in C,
exit conditions can be coded more explicitly than with boolean flags.

C.J.Lo
ARPA : cjl at Indiana@CSNet-Relay
UUCP : ...!iuvax!cjl



More information about the Comp.lang.c mailing list