Thanks. My question is more on the general view (I use GCC on Linux, UNIX, but sometimes use ARM ADS on some projects as well).
I always worry about putting "break" in for loop and while loops because I am not sure if it is a bad practice (as using goto). So I only use it in case statements. Should I avoid "break" if possible?
Joe
Didn't find your answer? Ask the community — no account required.
S
Steve at fivetrees
No. By definition, it only ends the current loop - so it doesn't break structure. If, OTOH, it jumped across two or more constructs...
Steve
formatting link
formatting link
R
Rick Merrill
...
No, it's a good thing, if you know what I mean. But if you have a large set of cases, try to group those that end in 'break' together and those that end in 'return' together, et cetera. - RM
C
CBFalconer
... snip ...
break (and continue) preserve more information about loop states than does a blunt goto. Therefore it is easier to optimize code using them than code using goto.
As far as clarity is concerned, that is another matter IMO. I consider that gotos used only to replace the effects of break and continue would be clearer, because they would contain a clear identification of the place gone to, and that place would in turn be clearly identified with a sited label.
My advice is the same as for gotos - use them when needed, but avoid the need. I think your instincts are sound. Now you may have some reasons to back them up.
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
USE worldnet address!
G
George Neuner
Using break is an accepted practice. Stylistically you should take care to call attention to a break with a comment or by setting it off in braces because the next person reading your code could easily overlook it.
Someone once suggested to me that, rather than use "break", it was clearer to set a loop termination flag and use "continue" to force the exit. This isn't totally crazy because in many CPU architectures it is more efficient to code the test at the end of a loop and branch backward to the beginning if necessary. Using "break" is more efficient for exiting a single loop because it is unconditional, but the "continue" technique can easily be used to exit from nested loops whereas the "break" cannot.
I have only rarely used the "continue" method, but I always keep it in mind in case it happens to fit the pattern of the code I am writing.
George
Send real email to GNEUNER2 at COMCAST o NET
R
robin.pain
Yes, naughty is loveable, but saintliness is... don't ya just hate it! On the otherhand, a saint usually a pain in the arse (to "authority").
Cheers Robin
S
Steve at fivetrees
(excluding
Completely agreed.
In many situations where such dilemmas occur, however, a (guess what!) state machine will improve readability and hence maintainability, and make such issues irrelevant.
Steve
formatting link
formatting link
A
Alan Balmer
OTOH, the goto is not constrained to have any meaning with respect to the construct it's used in, where a break or continue has a clearly defined effect with respect to that construct.
Al Balmer
Balmer Consulting
removebalmerconsultingthis@att.net
T
Thomas Wicklund
In general, error recovery tends to take a nice clean architecture and make a mess of it. One reason why too many people ignore it (along with the fact that in many cases there isn't a clean recovery method possible).
Any code (error recovery or other) needs to be judged by clarity and readability. It's possible to create very unclear but properly structured code or clear code with gotos.
Tom
T
Thomas Wicklund
Worse than that, they may start trusting the assembler or even trust that the hardware works as defined. :-)
Tom
P
Paul Carpenter
Of course they would never do that :-^ They have only be worried about creating a good product that works for the last n years of course :-)
Note seen comment recently on computer security in IEEE Computer Society magazine this month, where someone quotes about PC Firewall products that not enough testing and rushing to market abounded.
Paul Carpenter | paul@pcserv.demon.co.uk
Main Site
GNU H8 & mailing list info.
For those web sites you hate.
S
Scott Moore
Humm, found the code for my BIOS, concerning memory testing: