🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: use of the break statement

#2208
From: Jerry Coffin
Date: Fri, 08 Mar 1996 23:03
63 lines
3100 bytes
In article <4hlalm$4i6@solutions.solon.com>, rog@ohm.york.ac.uk says...

[ ... ] 

> 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.

Hmm...it could be.  If at all reasonable, I prefer to keep the entire loop 
termination together, particularly if either condition is likely to break the loop.  
I particuarly like the truly degenerate form:

while(1)
or:
for(;;)

with the exit from the loop strictly through breaks/gotos, etc.

When possible, I prefer to reserve `break' for more or less exceptional conditions 
rather than being the expected exit from the loop.  For instance, if malloc fails, 
there might be no sensible alternative to exiting a loop that keeps trying to 
allocate more memory.  However, it's quite unusual for malloc to fail, and when/if 
it does, it's likely that the program will do all sorts of things that are quite 
unusual.

[ ... ] 
>   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 ?

It's pretty hard to give a solid judgement of something being "bad" or not, and 
without more information about the surrounding code, it changes from pretty hard to 
simply impossible, IMO.

My question in looking at a loop like those above would be whether you intend to 
finish the loop or not.  For instance, if your last loop is intended to process the 
entire `categ' array, but might quit if the system is judged `insane' or somesuch, 
then a break is likely reasonable.  OTOH, if the loop is really searching through 
the `categ' array, and the only time `i' equals `NUMSECTS' is if the user has asked 
to, say, delete something that can't be found because it doesn't exist, then I'd 
like it a lot less.

In short, IMO, it's largely a matter of the loop condition accurately reflecting 
the basic idea of when the loop will normally exit.  Even if it complicates reading 
that line of code, if it makes the overall intent of that section of code more 
obvious, it's likely worthwhile.
    Later,
    Jerry.
Technical Details
Message-ID: <4hqeck$1kt@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><4hlalm$4i6@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: jcoffin@rmii.com (Jerry Coffin)
Newsgroups: comp.lang.c.moderated
Subject: Re: use of the break statement
Date: 8 Mar 1996 17:03:47 -0600
Organization: TAEUS
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4hqeck$1kt@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><4hlalm$4i6@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: MicroPlanet News32 v1.00 Beta Build 302