🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: use of the break statement

#2117
From: Steven Huang
Date: Thu, 29 Feb 1996 14:39
66 lines
3096 bytes
In article <4h2s54$o3@solutions.solon.com> bill_winn@acm.org (Bill Winn) writes:
>In article <4gnbcp$biq@solutions.solon.com>, news@dcs.warwick.ac.uk 
>composed...
>>In code that I write, and see, every day, break is very commonly used
>>to exit a loop.  In fact, I sometimes have a break being the /only/ way
>>to exit a loop.  (Something like perl's split loop body would be a nice
>>addition to the language.)  I don't consider it to be in any way bad
>>style.

>>What does this standard say about continue and goto?

>I beg to differ.  break, used in the context of loops, is nothing more than 
>a glorified goto statement.  If you have conditions that force a loop to 
>stop executing why not just have a flag that is set when the loop is to 
>terminate?  Sure, you have to go to the top of the loop (in the case of for 
>and while) to test for the condition, but I would venture a guess that doing 
>such will not cause your system to fail.

>break is *never* the only way that a loop can stop executing.  There is 
>*always* one or more conditions attached to the execution of the break, and 
>those conditions may server, just as easily, to set the flag described 
>above.

>Please, no arguments like "my system has hard real-time constraints and 
>can't afford the extra cycles" or "I have memory constraints and can't 
>afford to set aside room for the flag."  If you have these arguments against 
>using the flag, I'll *bet* that there are other areas of your system that 
>need to be optimized.

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?

-- 
Steven Huang (sthuang@hns.com), not speaking for Hughes Network Systems
?tfel ot thgir morf gnidaer uoy era yhw tuB
Technical Details
Message-ID: <4h4dq5$8o1@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>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: sthuang@hns.com (Steven Huang)
Newsgroups: comp.lang.c.moderated
Subject: Re: use of the break statement
Date: 29 Feb 1996 08:39:01 -0600
Organization: Hughes Network Systems Inc.
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4h4dq5$8o1@solutions.solon.com>
References: <4gfcl7$2ls@solutions.solon.com> <4gnbcp$biq@solutions.solon.com> <4h2s54$o3@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com