I rarely ever see continue used in a program. There's not many times when it'd be needed, but even in certain times other ways are usually used. Is it just not common, or does it have some drawbacks or slowdowns?
I rarely ever see continue used in a program. There's not many times when it'd be needed, but even in certain times other ways are usually used. Is it just not common, or does it have some drawbacks or slowdowns?
I use it whenever needed... some people say it fits in with goto in the fact that it makes for spaghetti code, but I tend to disagree...
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
Ah, cool, yeah I think continue is a handy little statement, but I was thinking maybe it had some problems that kept many people from using it.
nah... I have yet to hear any good arguments as to why not to use it (but if anybody has any, I'm more than willing to listen)
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
It's unstructured like goto, which is considered by some to be bad. As far as slow downs, the only case I can think of is if the compiler has trouple detecting the loop. The unstructured jump to the top might affect how the compiler detects inner and outer loops.I rarely ever see continue used in a program. There's not many times when it'd be needed, but even in certain times other ways are usually used. Is it just not common, or does it have some drawbacks or slowdowns?
You probably don't see it often because the best cases to use it are either in empty loops, where you'd normally code
using continue, you'd codeCode:while(condition) ;
or in short cut offs likeCode:while(condition) continue;
Code:while(condition) { if (cond2) continue; // followed by long section of code }
Yeah I was gonna use it for a cutoff like you exampled, but it's for a game, so I wanted to make sure it wouldn't be slow or anything.
I use it a lot in short cutoffs as okinrus showed. I prefer to use continue than to add a new nesting level.
It has its uses, instead of making a large "if" or a deeply nested "if":
Clean, clear, readable!Code:for(int i = 0; i < NrOfObjects; i++) { if(ObjectTooBig(i)) continue; if(ObjectTooSmall(i)) continue; if(ObjectTooExpensive(i)) continue; if(ObjectTooUgly(i)) continue; if(ObjectTooFat(i)) continue; if(ObjectTooHilarious(i)) continue; if(ObjectTooDark(i)) continue; if(ObjectTooLight(i)) continue; if(ObjectTooWeird(i)) continue; if(ObjectTooNormal(i)) continue; UseObject(i); break; }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Code:for ( i = 0 ; i < numPosts ; i++ ) { if ( posterUsesVoidMain(i) ) continue; if ( posterUsesFflushStdin(i) ) continue; if ( posterUsesGetsForInput(i) ) continue; if ( posterCodeFormattingSucks(i) ) continue; if ( posterQuotesSchildt(i) ) continue; if ( !posterReadFAQ(i) ) continue; if ( posterCrossPosting(i) ) continue; if ( posterBumpingThread(i) ) continue; if ( posterUrgencyLevel(i) > 0 ) continue; if ( !(posterStatedCompiler(i) && posterStatedOperatingSystem(i) ) ) continue; AnswerPost(i); }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
If I may, here is an example from C99:EXAMPLE 1 It is sometimes convenient to jump into the middle of a complicated set of statements. The following outline presents one possible approach to a problem based on these three assumptions:
- The general initialization code accesses objects only visible to the current function.
- The general initialization code is too large to warrant duplication.
- The code to determine the next operation is at the head of the loop. (To allow it to be reached by continue statements, for example.)
Code:/* ... */ goto first_time; for (;;) { // determine next operation /* ... */ if (need to reinitialize) { // reinitialize-only code /* ... */ first_time: // general initialization code /* ... */ continue; } // handle other operations /* ... */ }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*