Loop issue

This is a discussion on Loop issue within the Windows Programming forums, part of the Platform Specific Boards category; Code: for(int i = 0; i <= x; i++) { for(int j = 0; j <= y; j++) { // ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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,859
    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,434
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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
    21,935
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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,189
    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 08:14 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  11. #11
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,668
    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 04:54 PM.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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, 09: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, 06: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

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