🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: use of the break statement

#2122
From: Chris Wilkins
Date: Fri, 01 Mar 1996 15:31
61 lines
3322 bytes
In article <4h2s54$o3@solutions.solon.com> bill_winn@acm.org (Bill Winn) writes:

>I beg to differ.  break, used in the context of loops, is nothing more than 
>a glorified goto statement.

I can't follow your logic here. It is true that break and goto both cause a 
jump in program execution, but so does a for loop or an if statement. Are for 
loops and if statements nothing more than glorified goto statements?

Surely, the thing that is characteristic of the goto statement is that 
it changes program execution in an unrestricted fashion. This means that you 
can violate the normal block structure of the program. You can jump in and out 
of loops or conditionals. The break statement, on the other hand, doesn't 
violate the block structure of the program at all. It is quite clear what it 
does: it exits immediately from the current loop. It can't jump you into 
another loop that you're not already in and it can't jump you into a 
conditional statement.

The break statement seems to me to be the most natural coding construct for 
the situation where some sort of exception arises in the loop creating the 
need to exit early. I much prefer the directness of the break statement to any 
cryptic constructs such as fudging the loop condition or creating an 
auxilliary 'loop termination' flag variable. A loop termination flag has all 
the inelegance of the break statement with none of the efficiency. You still 
have to hunt around looking for where and when the flag is set (and you 
don't get any assistance from colour coding editors).

I can understand if someone really feels strongly about the break statement 
that they might require that every loop with a break statement have a comment 
above it indicating the presence of the break. i.e.:

/* following loop may be terminated early by break */
for(...) {
...
}

But banning the break statement seems to me to be pushing people in the 
direction of things a whole lot worse.

Actually, to be truthful, I actually like the break statement in some contexts 
even when it isn't strictly necessary. I like reading loops of the form:

for(i=0; i<len; i++) 
    if(s[i]!=' ') break;

Rather than:

for(i=0; i<len && s[i]!=' '; i++);

The first example conveys to me more directly what's going on. 

>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.

I prefer the break simply because I find it clearer, more natural and more 
legible than the possible alternatives.

Chris
Technical Details
Message-ID: <4h7592$4k0@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: chrisw@leland.stanford.edu (Chris Wilkins)
Newsgroups: comp.lang.c.moderated
Subject: Re: use of the break statement
Date: 1 Mar 1996 09:31:46 -0600
Organization: Stanford University
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4h7592$4k0@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
X-Newsreader: Trumpet for Windows [Version 1.0 Rev A]