🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: use of the break statement

#2153
From: Gene Wirchenko
Date: Sat, 02 Mar 1996 14:40
56 lines
2544 bytes
kgold@watson.ibm.com (Ken Goldman) wrote:

[snip]

>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);
>          
>Yes, I suppose that I could set a success flag, test for it in the loop,
>and then conditionally decrement i before the return.  Would that really
>be more clear than the break?

     While I agree that break can be useful and prefer it to the
monstrosity of flags and testing flags (and flagging interest in the
resulting code <g>), your example could be better coded.  for is meant
for when the number of iterations is known.  You ought to have used
while as in:
          i=0;
          while (i<MAX && try_something(i)!=SUCCESS)
             i++;
          return i;
Also, the braces in your example are all redundant.
     Note that the proc call won't be done if i<MAX fails.  This
behavior of && is guaranteed by the Standard.

>(I'm not arguing performance.  I agree that generally, clarity of
>code is more important.)

     I've done a lot of xBASE coding and found loop to be very useful.
It is similar to break in application and it means skip to the loop
control evaluation.  This may or may not terminate the loop.
     It is very useful when you have cascading tests.  Each of these
would require another level of nesting if flags are used.  Since the
tests generally are of about equal importance, why nest them?

>-- 
>Ken Goldman   kgold@watson.ibm.com

Sincerely,

Gene Wirchenko

C Pronunciation Guide:
     y=x++;     "wye equals ex plus plus semicolon"
     x=x++;     "ex equals ex doublecross semicolon"
Technical Details
Message-ID: <4h9mkb$gt8@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>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: genew@mindlink.bc.ca (Gene Wirchenko)
Newsgroups: comp.lang.c.moderated
Subject: Re: use of the break statement
Date: 2 Mar 1996 08:40:11 -0600
Organization: MIND LINK! - British Columbia, Canada
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4h9mkb$gt8@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: Forte Free Agent 1.0.82