Article View: comp.lang.c.moderated
Article #2147Re: use of the break statement
From: Jerry Coffin
Date: Fri, 01 Mar 1996 23:33
Date: Fri, 01 Mar 1996 23:33
30 lines
1487 bytes
1487 bytes
In article <4h75gn$4rq@solutions.solon.com>, kgold@watson.ibm.com says...
[ much discussion elided ]
> Here's when I use "break" to get out of the for loop. Polite
> criticism is welcome.
>
> The problem with going back to the top of the loop to check a flag
> is that the loop counter is processed before the test. For example:
>
> /* returns the number that suceeded, returns MAX if failed */
>
> 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 */
}
Which is more readable often depends on the circumstances though. In addition, if
the condition being tested happens in the middle of the loop instead of the top
like it is here, this transformation may not be possible.
Later,
Jerry.
Technical Details
Message-ID:
<4h81fi$amo@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><4h75gn$4rq@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: 1 Mar 1996 17:33:06 -0600 Organization: TAEUS Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4h81fi$amo@solutions.solon.com> References: <4gfcl7$2ls@solutions.solon.com><4gnbcp$biq@solutions.solon.com><4h2s54$o3@solutions.solon.com><4h75gn$4rq@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com X-Newsreader: MicroPlanet News32 v1.00 Beta Build 302 (30 Day Trial)