🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2191

Re: use of the break statement

#2191
From: Roger Peppe
Date: Thu, 07 Mar 1996 00:29
78 lines
2627 bytes
On 1 Mar 1996 17:33:06 -0600, Jerry Coffin <jcoffin@rmii.com> wrote:
>In article <4h75gn$4rq@solutions.solon.com>, kgold@watson.ibm.com says...
>>     for (i = 0 ; i < MAX ; i++) {
>>         if (try_something(i) == SUCCESS) {
>> 	    break;
>>         }
>>     }
>>     return(i);

>That looks equivalant to:
>
>	for (i=0;i<MAX && try_something(i)==SUCCESS; i++) {
>		/* other stuff */
>	}

i prefer the first form.

the point being that certain for loop constructs
are very well known idioms with immediately obvious
properties. if the form is violated, it immediately
imposes a `what exactly is going on here ?' time penalty
when trying to understand the code.

exact termination conditions are extremely important,
and i think the use of constructs of the form:

  for (well understood looping idiom)
    if (difficult condition)
      break;

make for more robust, more easily verified code.

i went and looked through my code for examples, so
here are a few.

  for (j = 0; j < NUMVARNAMES; j++)
    if (!strcmp(varnames[j].nm, args[i].begin))
      break;

  for (s = nm; *s; s++)
    if (*s != ' ' && *s != '\t' && *s != '\n')
      break;

  for (pt = txhash[HASHNUM(tid)]; pt; pt = pt->next)
    if (pt->tid == tid)
      break;

  for (i = argc - 1; i >= 1; i--)
    if (!strchr(argv[i], SUBSCHAR))
      break;

  for (t1 = s1, t2 = s2; *t1 && *t2; t1++, t2++)
    if (*t1 != *t2)
      break;

  for (i = 0; i < redraw_num; i++)
    if (dpt->complete_area || intersects_rect(&dpt->bbox, &redraw_area[i]))
    {
      add_point(drawlist, dpt);
      break;
    }

  for (i = 0; attrmask[i].calcmode != -1; i++)
    if (ctype == attrmask[i].calcmode)
      break;

  for (j = 3; j > 0; j--)
    if (n & (0xff << (j * 8)))
      break;

  for (i = 0; i < NUMSECTS; i++)
    if ((categ[i].whichexe & prognum) && (*categ[i].isfn)(info, t))
      break;

is this a bad way to write code ?

  cheers,
    rog.
Technical Details
Message-ID: <4hlalm$4i6@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4gfcl7$2ls@solutions.solon.com><4gnbcp$biq@solutions.solon.com><4h2s54$o3@solutions.solon.com><4h75gn$4rq@solutions.solon.com> <4h81fi$amo@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: rog@ohm.york.ac.uk (Roger Peppe)
Newsgroups: comp.lang.c.moderated
Subject: Re: use of the break statement
Date: 6 Mar 1996 18:29:42 -0600
Organization: Department of Electronics, University of York, UK.
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4hlalm$4i6@solutions.solon.com>
References: <4gfcl7$2ls@solutions.solon.com><4gnbcp$biq@solutions.solon.com><4h2s54$o3@solutions.solon.com><4h75gn$4rq@solutions.solon.com> <4h81fi$amo@solutions.solon.com>
Reply-To: rog@ohm.york.ac.uk
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: slrn (0.8.4)