Thread: breaking multiple loops

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    breaking multiple loops

    Hi,

    I would like to know if it is possible to break a loop from inside another loop. In Java this is possible like so:

    Code:
    myloop:
    for(; ; )
      for(; ; )
        break myloop;
    Is there a C equivalent of this

    Of course I can just create another variable and set it to a particular variable in the inner loop, however this results in an extra comparison on every iteration of the outer loop.

    Thanks

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You could use the evil GOTO command

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    well...from what I've heard and read about GOTO, and the fact that you call it EVIL, I'll probably stay away from it. Thanks anyways.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Does this work? Probably breaks a lot of style rules.


    Code:
    for (i = 1; i < I_MAX; i++)
     for (j = 1; j < J_MAX; j++)
        if (needToBreakBoth()) {
           i = I_MAX; 
           break;
        }
    K&R says use a goto or use an extra variable. They say this case is one of the few exceptions where you might possibly consider using a goto.
    Last edited by major_blagger; 02-23-2004 at 12:11 PM.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    That would work major_blagger however then you are wasting the value of i.

    I said evil goto because a lot of people think its horrible. The fact is that goto has it's uses. Granted most of the time their are ways around it but in cases like this it does make sense to use it.

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    The fact is that goto has it's uses. Granted most of the time their are ways around it but in cases like this it does make sense to use it.
    This situation is one of those uses. The usual suggestion of using a status variable is both wasteful and clutters the code. So many people find this to be perfectly reasonable.
    Code:
    while (condition1) {
        while (condition2) {
            if (something)
                goto end;
        }
    }
    end:
    I have no preference either way so that I might stay out of the line of fire, but a good solution that clarifies the code in some cases and removes the need for goto is to wrap the inner loop in a function.
    Code:
    int func()
    {
        while (condition2) {
            if (something)
                return 1;
        }
    
        return 0;
    }
    
    ...
    
    while (condition1) {
        if (func())
            break;
    }
    Of course, setting up parameters and such can also be a bother at times. So aside from more careful design, the problem remains to be solved effectively for all cases. Remember that everything is relative. People who refuse to use goto "because it's evil" clearly don't have an open mind about programming. If a tool can solve the problem better than others in a certain case then there is nothing wrong with using that tool.

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Votes for,

    Code:
    while (condition1)
        while (condition2)
           if (cond)
                goto label;
    
    label:
    I've been programming C for approx 14 months and I've yet had
    good reason to use the goto keyword but in this particular case
    I would certainly consider it.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thank you for rehasing what I said Edward

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Not me. I'd still opt for:
    Code:
    while (condition1)
    {
        while (condition2)
        {
           if (cond)
           {
                condition1 = false;
                break;
           }
        }
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    well, thanks for all the input on this one, I guess I'll try the GOTO, it'll be first for me in C.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    WaltP:
    But what happens when you have
    Code:
    while (condition1)
    {
        while (condition2)
        {
           if (cond)
           {
                condition1 = false;
                break;
           }
        }
        cout << "I don't want to display this if !condition1\n";
        cout << "I don't want this either\n";
        cout << "Or this." << std::endl;
    }
    Then you'd have to do the other test case again (either another variable - or do something to make the original condition1 false, potentially causing other problems), and if you're really strapped for speed, you won't want that. Just as Edward said.
    The usual suggestion of using a status variable is both wasteful and clutters the code.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Hunter2
    WaltP:
    But what happens when you have
    Code:
    while (condition1)
    {
        while (condition2)
        {
           if (cond)
           {
                condition1 = false;
                break;
           }
        }
        if (condition1)
        {
            cout << "I don't want to display this if !condition1\n";
            cout << "I don't want this either\n";
            cout << "Or this." << std::endl;
        }
    }
    It's not elegant, but what you are trying to do is nonelegant (inelegant?) in the first place.

    Or as was mentioned before, put this loop in a function and return instead of break.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, but then you're testing for a condition again, potentially killing your program if it's a really crucial speed-spot. Granted, if it isn't crucial, it probably won't make much of a difference. But I'm against putting the loop in a function. That would mean that if you need to access data from outside the loop, you'd either have to make it global or pass it as parameters, which would mean processing time setting up the stack; Besides which, it means you'd have to do the jump-to-other-section-of-code-then-jump-back-to-where-you-were thing when reading the code, potentially damaging readability. And besides which, it takes a lot more effort to do, as Edward said.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 12:01 PM
  3. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  4. Breaking a for loops when result reached
    By PaulStat in forum C++ Programming
    Replies: 5
    Last Post: 08-15-2003, 09:00 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM