Using "continue" (basic language problem)

This is a discussion on Using "continue" (basic language problem) within the C++ Programming forums, part of the General Programming Boards category; If I have something like: Code: for(int i=0; i<100; i++) { for(int n=0; n<100; n++) { if(n==i) continue; ... // ...

  1. #1
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120

    Question Using "continue" (basic language problem)

    If I have something like:
    Code:
    for(int i=0; i<100; i++)
    {
       for(int n=0; n<100; n++)
       {
          if(n==i)
             continue;
    
          ... // more stuff
       }
       ... // more stuff
    }
    Can I, with a single line of code, make the "continue;" skip to the end of the outer loop (i.e. rather than just the loop it's in).

    thanks in advance,

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Is not a single line, but...(Why you want a single-line solution anyway?)
    Code:
    bool found = false;
    for ( int i = 0; i < 100; i++ )
    {
       for ( int n = 0; n < 100; n++)
       {
          if ( n == i ) {
             found = true;
             continue;
          }
    
          ... // stuff to skip
       }
        
       if ( found )
            continue;
       ... // more stuff to skip
    }
    Or lookup "goto", never used that before so don't know how to use it
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Many thanks. That's what I've been doing, but I was wondering if there was a slimmer way of doing it as it's in a seriously nested loop which needs to be as efficient as possible. A single line solution would be great and I seem to remember seeing one for break, so I figured there'd be one for continue. I'm probably imagining things though!

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Well then lookup "goto", it'll be something like this
    Code:
    for ( int i = 0; i < 100; i++ )
    {
       for ( int n = 0; n < 100; n++)
       {
          if ( n == i ) {
             goto LABEL_A;
    
          ... // stuff to skip
       }
      
       ... // more stuff to skip
       LABEL_A
    }
    I don't know the exact construction. And note that the use of "goto" is a sensitive issue so I don't know if this one is one of the correct implementations of "goto"
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >And note that the use of "goto" is a sensitive issue
    You can say that again. The ignorant masses tend to drive controversy.

    >so I don't know if this one is one of the correct implementations of "goto"
    That is one of the "acceptable" uses. But using goto feels inelegant; I would spend a few grey cells trying to restructure the program so that it's unnecessary.
    My best code is written with the delete key.

  6. #6
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Thankyou Alphaoid. I agree with Prelude though, and I've just stuck with what I've got and slimmed everything else down as much as possible (just bought Effective C++ so on a mission . I got the 3-4 second loop in question, down to around half a second. It's amazing what crap you can write when you write code ad lib and without real design!
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Replies: 8
    Last Post: 10-24-2007, 05:46 PM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. Visual Basic Adodc Problem
    By rahat in forum Windows Programming
    Replies: 1
    Last Post: 01-20-2002, 05:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21