Thread: continue statement

  1. #1
    myNegReal
    Join Date
    Jun 2005
    Posts
    100

    continue statement

    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?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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

  3. #3
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    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.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    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?
    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.

    You probably don't see it often because the best cases to use it are either in empty loops, where you'd normally code
    Code:
    while(condition)
          ;
    using continue, you'd code
    Code:
    while(condition)
           continue;
    or in short cut offs like
    Code:
    while(condition) {
          if (cond2)
                continue;
    
          // followed by long section of code
    
    }

  6. #6
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    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.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I use it a lot in short cutoffs as okinrus showed. I prefer to use continue than to add a new nesting level.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It has its uses, instead of making a large "if" or a deeply nested "if":
    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;
    }
    Clean, clear, readable!
    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.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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:
    1. The general initialization code accesses objects only visible to the current function.
    2. The general initialization code is too large to warrant duplication.
    3. 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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  3. continue statement causing an unexpected error
    By drb2k2 in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2003, 06:46 AM
  4. continue statement
    By minesweeper in forum C++ Programming
    Replies: 4
    Last Post: 12-08-2002, 05:07 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM