can i use continue inside if loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shiniskumar
    New Member
    • Feb 2007
    • 58

    #1

    can i use continue inside if loop?

    b1: if(){

    sstmts;
    continue b1;
    }
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    As far as I know the inside of loops is the ONLY place you can use a continue. I hope you realsie, however, that continue means "lets get on with the next iteration"
    In other words if you had the following code:
    Code:
    for(a=start; a< finish; a=a++)
    {
      myValue=importantValue();
      if(myValue==SOME_IMPORTANT_CONSTANT)
      {
        continue;
      }
      doSomething();
    }
    then "doSomethin g" would NOT be executed in the case tahat we reached the continue statement.
    If you want something more like "if(some case) doNothing else doSomething", then consider rephrasing the if to be "if (!(some case))" OR alternatively, have an empty if (whioch is not considered good practice but is syntactically legal)
    Code:
    if(!dontCareValue)
    {
      betterDoSomething();
    }
    
    
    /* Or a not so clean solution */
    
    
    if(dontCareValue) /* do nothing for if (we could omit {} and use ; if we really wanted */
    {}
    else
    {
      betterDoSometing();
    }

    Comment

    • hirak1984
      Contributor
      • Jan 2007
      • 316

      #3
      you can put continue inside if( rather you should put it inside an if)

      only when the if itself is in any loop.
      Originally posted by shiniskumar
      b1: if(){

      sstmts;
      continue b1;
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        May I just say that it is very bad practice to use continue statements.
        Last edited by r035198x; Mar 5 '07, 09:42 AM. Reason: spellings!

        Comment

        • DeMan
          Top Contributor
          • Nov 2006
          • 1799

          #5
          You certainly can, and reading this post I was wondering whether you've ever come accross a case where you need it (I've used break WAY too many times, but never continue)

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            I used continue in Chess. Basically, when there's an error in getting the move from a player, I want to skip any statements that will use this bad input, but I don't want to end the game. I use continue to go back to the start of the loop where I get input.

            You could also use it in the following scenario: Check the numbers 1 through 10 to see if a number is prime. IF it is prime, do x number of new calculations on it, ELSE, don't do anything - but keep checking. You could use a large if statement to make sure the operations are only done IF x is prime, or you could use a continue statement.

            Comment

            • ria3
              New Member
              • Mar 2007
              • 8

              #7
              You could make several if loops inside a while loop.

              Comment

              Working...