Thread: How to exit from nested loops?

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    182

    Question How to exit from nested loops?

    Hi everybody.

    If I have nested loops, and I'd like to exit all of them given a condition,
    How do I break out?

    Code:
    for (x = 0; x < lim0; x++){
        for (x1 = 0; x1 < lim1; x1++){
            for (x2 = 0; x2 < lim2; x2++){
                 if (contidion)
                     break; // this exit the inner loop only - 
           }
        }
    }
    I don't know if C has got some exit or anything that suites the case,
    the only alternatives I see at the moment are return if I put the nested
    loops in a function, goto that is badly considered in modern world
    or to change the whole structure of the program, and that could be quite heavy
    to digest.

    Or I could use a workaround:
    Code:
    for (x = 0; x < lim0; x++){
        for (x1 = 0; x1 < lim1; x1++){
             for (x2 = 0; x2 < lim2; x2++){
                   if (contidion){
                       break_x1 = 1;
                       break_x = 1;
                       break; // this exit the inner loop only
                    }
             }
             if (break_x1) break;
         }
          if (break_x) break;
    }

    Any alternative in standard C99?
    Last edited by frktons; 07-17-2010 at 05:43 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Use goto. It's the right place.
    Code:
    for (x = 0; x < lim0; x++){
        for (x1 = 0; x1 < lim1; x1++){
             for (x2 = 0; x2 < lim2; x2++){
                   if (contidion)
                       goto end_nested_loop; 
             }
         }
    }
    end_nested_loop:
    Last edited by Bayint Naung; 07-17-2010 at 05:14 AM.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by Bayint Naung View Post
    Use goto. It's the right place.
    Code:
    for (x = 0; x < lim0; x++){
        for (x1 = 0; x1 < lim1; x1++){
             for (x2 = 0; x2 < lim2; x2++){
                   if (contidion)
                       goto end_nested_loop; // this exit the inner loop only - 
             }
         }
    }
    end_nested_loop:
    What about the alternatives I was thinking about?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I don't see any alternative to do so in C99.
    Since there's already something to solve it:goto.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by Bayint Naung View Post
    I don't see any alternative to do so in C99.
    Since there's already something to solve it:goto.
    What about the multiple break I implemented? Somebody
    could even be happier I didn't use goto.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I prefer goto approach. It seems 'cleaner' to me.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by Bayint Naung View Post
    I prefer goto approach. It seems 'cleaner' to me.
    It looks cleaner to me as well, I was trying to figure what goto-forbidden
    C-guys would use in a situation like this.
    Last edited by frktons; 07-17-2010 at 05:48 AM.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You could put the loops into a function and then use return instead of goto.
    Code:
    int myLooper(int lim0, int lim1, int lim2)
    {
       int x, x1, x2;
    
       for (x = 0; x < lim0; x++){
           for (x1 = 0; x1 < lim1; x1++){
               for (x2 = 0; x2 < lim2; x2++){
                    if (contidion)
                        return(3);
              }
           }
       }
       return(0);
    }

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    But don't forget you'll need to pass other parameters that are used in loops.

    Code:
    int foo;
    char bar[100];
    for (x = 0; x < lim0; x++){
        bar[x] = ...;
        for (x1 = 0; x1 < lim1; x1++){
            array[x][x1] += foo + ...;
            for (x2 = 0; x2 < lim2; x2++){
                 if ( estimate ( result, THRESHOLD) > EPSILON ) 
                     break; // this exit the inner loop only - 
           }
        }
    }

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    My first guess would be to not allow loops to get nested that deeply. I can't imagine why you would want to do that or that there isn't a better or alternative approach to nesting that deep.

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Scientific application often involve models of 3D space that are usually represented by 3D arrays, so 3 nested loops is hardly unusual.

    goto is the simplest way out, but if you are hard against goto the best way is to set flags in each loop to tell the next level loop to break if the inner one does.

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Scientific application often involve models of 3D space that are usually represented by 3D arrays, so 3 nested loops is hardly unusual.
    Regardless of the application or task at hand nested loops can always be unrolled or separated into smaller loops. Games and rendering apps use a lot of math and transformations as well and they do not require 3 nested loops.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could rebuild your car's body with carbon fiber and exotic alloys too, but I'm not going to do it.

    Nested loops may not be as elegant, or even quite as fast, but the logic is clear, and the code is easy to write and understand. Those who want to choke on a goto from inside a nested loop, probably have a waiting line of things they want to choke on.

    To me, this is *THE* place to use a goto.

  14. #14
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    As long as your code doesn't turn into spaghetti, and instead it becomes simpler, i see no reason why goto shouldn't be used!
    Devoted my life to programming...

  15. #15
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Quote Originally Posted by Bubba View Post
    Regardless of the application or task at hand nested loops can always be unrolled or separated into smaller loops. Games and rendering apps use a lot of math and transformations as well and they do not require 3 nested loops.
    If you want your software to be readable and possible to upkeep then why would you want to sacrifice the simplicity of nested loops? The use of goto is almost always discouraged because you lose readibility and it can become very hard to maintain, but here the opposite is true, whereas reducing the loops like you suggest does just that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  2. Displaying a table using nested loops
    By leviterande in forum C Programming
    Replies: 13
    Last Post: 09-29-2009, 04:42 PM
  3. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  4. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  5. nested for loops
    By akub3 in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 06:21 AM