Article View: comp.lang.c.moderated
Article #2130Re: use of the break statement
From: J. Kanze
Date: Fri, 01 Mar 1996 15:34
Date: Fri, 01 Mar 1996 15:34
60 lines
2856 bytes
2856 bytes
In article <4h4dq5$8o1@solutions.solon.com> sthuang@hns.com (Steven
Huang) writes:
|> You're right in saying that 'break' isn't the only way to exit a loop,
|> yet there are some constructs that are just as awkward if you need to
|> set flags. For instance,
|> while (... && !flag) {
|> ...
|> if (...)
|> flag = TRUE;
|> ...
|> if (!flag) {
|> A();
|> }
|> }
|> is a common thing you might see. Against:
|> while (...) {
|> ...
|> if (...)
|> break;
|> ...
|> A();
|> }
|> The second one is shorter, and the break is actually a more obvious
|> way to exit the loop. Also, it prevents extra test cases which must
|> be done if you have code that shouldn't be executed after the test
|> for 'break'. Just like 'goto', 'break' has its place. If that loop
|> executes a few thousand times (for example, parsing a text file
|> character by character seems a common loop), skipping an extra 'if'
|> may not be dramatic, but it does help. Just because there are
|> alternatives to 'break' doesn't mean one shouldn't use 'break', don't
|> you agree?
I agree that the extra test in the loop is a bother, but...
The first version has the advantage that all of the loop termination
conditions are immediately visible at the top of the loop. It also has
the advantage that if I, for example, add code which mallocs memory at
the top of the loop, and add the corresponding free at the end, nothing
breaks.
The second version seems to correspond to what naive users think `while'
does.
I'll admit that I don't like either. When pushed, I'll use the first
version, but in general, I try and design so that the while (or for)
expresses the loop invariant in its condition, and that this invariant
is true everywhere in the loop. It makes the code much easier to
understand. (Using `for' instead of `while' will often help here.
While difficult to not to break occasionally, a nice rule is to treat
loop control variables as constant withing the loop body.)
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
-- A la recherche d'une activité dans une region francophone
Technical Details
Message-ID:
<4h75do$4ok@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4gfcl7$2ls@solutions.solon.com> <4gnbcp$biq@solutions.solon.com> <4h2s54$o3@solutions.solon.com> <4h4dq5$8o1@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: kanze@gabi-soft.fr (J. Kanze) Newsgroups: comp.lang.c.moderated Subject: Re: use of the break statement Date: 1 Mar 1996 09:34:16 -0600 Organization: GABI Software, Sarl. Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4h75do$4ok@solutions.solon.com> References: <4gfcl7$2ls@solutions.solon.com> <4gnbcp$biq@solutions.solon.com> <4h2s54$o3@solutions.solon.com> <4h4dq5$8o1@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com