Thread: continue statement

  1. #1
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798

    continue statement

    I have just come across the conitnue statement, I have never before seen it used at all. could someone please verify that my understanding of it is correct. Say we have this code:

    Code:
    for (//setup of for loop in here)
    {
    
    if (condition 1)
    {
    continue;
    }
    
    if (condition 2)
    {
    //perform some tasks
    }
    
    }
    Am I correct in thinking that if condition 1 is true, the second 'if' statement will never be performed?

    Also what would occur if the code was this?:

    Code:
    for (//setup of for loop in here)
    {
    
    if (condition 1)
    {
    //perform some tasks
    }
    
    if (condition 2)
    {
    continue;
    }
    
    }
    Thanks in advance for any help given.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you have a loop (for/while/do) and use continue, the rest of the code within the loop will be skipped and the loop restarts.
    So yes, you assumption is correct. What will happen in case 2 is the same as if you didn't place the continue there, since you have nothing after it (you can say that the compiler 'places a continue' at the end of every loop).

    This will increase X 1 step at a hand, but only print it when it reaches 10:
    Code:
    int X = 0;
    while(true)
    {
       X++;
    
       if(X < 10)
       {
          continue;
       }
    
       cout << X << endl;
       break;
    }
    Last edited by Magos; 12-07-2002 at 09:36 AM.
    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.

  3. #3
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Thank you very much

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    Well you find the continue keyword in C, Java, and C# as well.
    Mr. C: Author and Instructor

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I think you've also seen the break statement, it exit the loop, or if it's used in a switch statement it will skip the other cases.

    Code:
    switch( x )
    case 1:
    //statements
    break;
    case 2:
    ....
    ....
    so if x == 1 then it won't check for x == 2.
    none...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Continue and switch
    By camzio in forum C Programming
    Replies: 10
    Last Post: 10-04-2008, 08:31 AM
  2. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  3. continue statement
    By Ganoosh in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2005, 03:52 PM
  4. continue statement causing an unexpected error
    By drb2k2 in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2003, 06:46 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