Thread: continue and break!

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    42

    continue and break!

    Hi everyone!
    I'm currently learning about continue and break, Loop section...
    But the example the book shows is too complicated, and I can't understand it..

    Do you have a code, a link to a page, etc.. that shows the use of these two statements?

    I link to a page that shows a code with a little explaination would be great..

    Thank you all!

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    66
    Example of break (used to break out of the immediate loop). It will execute 100 times then break out of the while loop.

    Code:
    int i =1;
    while (i > 0)
    {
        cout << "Loop=" << i << endl;
        ++i;
        if (i > 100)
           break;
    }
    cout << "Done" << endl;
    Output:
    Loop=1
    Loop=2
    ...
    ...
    Loop=100
    Done



    Now example of continue, which says go back to start of loop and go on with execution:

    Code:
    int i =1;
    while (i > 0)
    {
        if (i % 2)
        {
          // Odd number, increment and resume the while 
          ++i;
          continue;
        };
    
        cout << "Loop=" << i << endl;
        ++i;
        if (i > 100)
           break;
    }
    cout << "Done" << endl;
    Output:
    Loop=2
    Loop=4
    Loop=6
    ...
    ...
    Loop=100
    Done



    DISCLAMER: The code is not written in the most efficient way, but done so to make it easier to read.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    42

    Thanks!

    Thanks man, your reply was really helpful!!
    I have a code which stops life!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Also, you can read through this lot...
    http://cboard.cprogramming.com/showt...threadid=30291

    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    42

    Talking thanks

    thanks hammer..
    I have a code which stops life!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  3. Display Lists (OpenGL)
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 06-05-2005, 12:11 PM
  4. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  5. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM