Thread: Loop issue

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    Loop issue

    Code:
    for(int i = 0; i <= x; i++)
    {
         for(int j = 0; j <= y; j++)
         {
              // Break point
         }
    }
    Hello

    When I have two loops like this and i need to break both loops from the inner loop how do I do that?

    I tried

    Code:
    for(int i = 0; i <= x; i++)
    {
         for(int j = 0; j <= y; j++)
         {
              break;
         }
    }
    continue;
    Apparantly didnt work...

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    int flag = 0;
    for(int i = 0; i <= x && !flag; i++)
    {
         for(int j = 0; j <= y; j++)
         {
              // On break condition:
              flag = 1;
              break;
         }
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by rags_to_riches View Post
    Code:
    int flag = 0;
    for(int i = 0; i <= x && !flag; i++)
    {
         for(int j = 0; j <= y; j++)
         {
              // On break condition:
              flag = 1;
              break;
         }
    }
    Actually solved it out already... THX anyways

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or
    Code:
    int flag = 0;
    for(int i = 0; i <= x; i++)
    {
    	for(int j = 0; j <= y; j++)
    	{
    		// On break condition:
    		flag = 1;
    		break;
    	}
    	if (flag) break;
    }
    Looks better IMO. Complex expressions for loops just confuse you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Or just let the loops exit on their own?

    Code:
    for(int i=0;i<x;i++)
    {
            for(int j=0;j<y;j++)
            {
                   //leave?
                   if(some condition)
                   {
                             i=x;
                             j=y;
                   }
             }
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Or, if you want to do it without introducing an additional variable;
    Code:
    for(int i=0;i<x;i++)
    {
            for(int j=0;j<y;j++)
            {
                   //leave?
                   if(some condition) goto out;
             }
    }
    out:
    Responses about the evils of goto will be summarily ignored.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's one example I can't agree on. Goto may have its uses, but this is not one of them.
    Using a flag or filling the loop conditions is much better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's one example I can't agree on. Goto may have its uses, but this is not one of them.
    Actually, I would consider this as one of the few valid uses of goto: to break from multiple nested loops. The effect is similiar to using a break, except that it requires the label to the placed right after the nested loops so as not to turn the thing into spaghetti code as goto is infamous for.

    The trade-off is therefore the same as a break: you may make it easier to understand because the loop condition need not be so complex, but you also introduce multiple points where the loop may end.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, case in point, there's nothing stopping you from using it, so choose at will. I think I may stay from it, though. Just my own opinion.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    The use of goto is not inherently bad, it just gets the blame for sloppy programmers that misuse/abuse it. This is one case though where it is clearly the more readable choice.
    Last edited by abachler; 01-03-2008 at 09:14 AM.

  11. #11
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    gotos being bad is a common mis-conception, they are like any other code, they are only bad if the programmer mis-uses them.
    I use them alot recently, they simplify code, makes it easier to manage, and has yet to give me any problems.

    And this really should be in the C or C++ forum, not Windows.
    Last edited by Yarin; 01-03-2008 at 05:54 PM.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    Using a flag or filling the loop conditions is much better.
    It is fully incorrect for timecritical loops. - This solution adds the additional flag check on each iteration of the outer loop (and may - depending on the implementation - add additional check on the inner loop) - adding CPU cycles and also resulting in higher rate of branch mispredicitons.

    So when the Profiler shows the performance problems for such loops the usage of goto could be the only possible solution...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    For a doubly nested loop I think goto is not really that necessary, and flag-management can be applied all over the gaff. However... if you had four or five separate loops like so:

    Code:
    for(){
      for() {
        do {
        while();
      }
      for() {
        while() {
          for() {
          }
          for() {
            while() {
              for() {
              }
            }
          }
        }
      }
    }
    You might talk about redesigning this process but flagging becomes a hindrance and to be honest it's more likely that that your flag management is going to become more difficult and buggy than goto screwing up, so long as you ensure that all dynamic memory is deleted etc (as it should be even if you're using break).

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you have 4-5 levels of looping - you can start thinking of creating functions for inside loops.
    And in this case the breaking outside the loop can be done simply by return
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Yarin View Post
    gotos being bad is a common mis-conception, they are like any other code, they are only bad if the programmer mis-uses them.
    I use them alot recently, they simplify code, makes it easier to manage, and has yet to give me any problems.

    And this really should be in the C or C++ forum, not Windows.
    Anyone who ever states that they use goto's "a lot" is definitely misusing them!

    vart: Explicitly filling the loop conditions (as per novacain's post) does not add an "additional flag check on each iteration of the outer loop".

    Wrapping it in a seperate function is usually the way to go.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. Memory issue with new[] and delete[]
    By Zarkhalar in forum C++ Programming
    Replies: 24
    Last Post: 08-07-2004, 07:45 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM