🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: use of the break statement

#2185
From: Andrew Dalgleish
Date: Thu, 07 Mar 1996 00:25
96 lines
2874 bytes
Compare snippet 'A':
long	l;
for (l = 0L; l < 1000000L; l++) {
  /* Stuff before test */
  if (condition) {
    break;
  }
  /* Stuff after test */
}

To snippet 'B':
long	l;
{
  /* Extra braces to limit scope of bBreak */
  BOOL	bBreak = FALSE;
  for (l = 0L; !bBreak && l < 1000000L; l++) {
  /* Stuff before test */
    if (condition) {
      bBreak = TRUE;
    } else {
      /* Stuff after test */
    }
  }
}

Example B may evaluate "bBreak" a million times.
It is *possible* that "condition" never evaluates TRUE, 
so you've evaluated "bBreak" a 1,000,001 times for nothing.
In most code I write, the break exit is the exception, not the rule.

Now compare snippet 'C':
long	l1;
long	l2;
for (l1 = 0L; l1 < 1000000L; l1++) {
  for (l2 = 0L; l2 < 1000000L; l2++) {
    /* Stuff before test */
    if (condition) {
      goto EXIT_BOTH_LOOPS;
    }
    /* Stuff after test */
  }
  /* More stuff after test */
}
EXIT_BOTH_LOOPS:;

To snippet 'D':
long	l1;
long	l2;
{
  /* Extra braces to limit scope of bBreak */
  BOOL	bBreak = FALSE;
  for (l1 = 0L; !bBreak && l1 < 1000000L; l1++) {
    for (l2 = 0L; !bBreak && l2 < 1000000L; l2++) {
      /* Stuff before test */
      if (condition) {
        bBreak = TRUE;
      } else {
        /* Stuff after test */
      }
    }
    if (!bBreak) {
      /* More stuff after test */
    }
  }
}

Pop quiz:
1. How many times is "bBreak" evaluated?
	Ans:	Anywhere from 5 to 1,000,003,000,002 times.
	Note:	If you evaluate "bBreak" after the loop count, 
		it's only 1,000,001,000,000 times.
2. How many times do we *really* need to evaluate "bBreak"?
	Ans:	0 times.

Contrived examples perhaps, but if you apply this small principle 
in *every* loop in your application it all adds up.

I have yet to see code which uses break/continue/goto which could not 
be re-written without them, but if used carefully they make the code 
clearer and faster.

Yes break/continue/goto can cause you heartache, but can help you too.

I wish Netscape's editor had bracket matching & syntax checking :-)

-- 
Andrew Dalgleish
Senior Software Engineer
Axon Research, Pty Ltd
6 Wallace Ave
Toorak, VIC
3142
AUSTRALIA
Tel:	+61-3-9826-5538
Fax:	+61-3-9824-0083
Email:	andrewd@axonet.com.au
Technical Details
Message-ID: <4hladl$4cr@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4gfcl7$2ls@solutions.solon.com> <4gnbcp$biq@solutions.solon.com> <4h81gh$aql@solutions.solon.com> <4h9ml2$gub@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: Andrew Dalgleish <andrewd@axonet.com.au>
Newsgroups: comp.lang.c.moderated
Subject: Re: use of the break statement
Date: 6 Mar 1996 18:25:25 -0600
Organization: Axon Research, Pty Ltd
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4hladl$4cr@solutions.solon.com>
References: <4gfcl7$2ls@solutions.solon.com> <4gnbcp$biq@solutions.solon.com> <4h81gh$aql@solutions.solon.com> <4h9ml2$gub@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Mailer: Mozilla 2.0 (Win95; I)