Thread: How to exit from nested loops?

  1. #31
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by Adak View Post
    Gladly will I share the "mindless programming" patent, but I'm keeping the "totally mindless programming" patent, for myself!

    One way I've worked with problems like this (too much data for an array), is to have a for loop inside the innermost while() loop. When the biggest array you can handle is full (by testing you know this already), you call the function that tests all the numbers.

    When the testing on those numbers is finished, control returns to the while loops, and they pick up where they left off, very neatly. I would not call the test function for every number - just for the whole string array, when you have 40,000 or whatever you can handle, in the string array, full.

    Upon it's return, you exit the for loop, and the counter is reset to 0 again. Works well, easy to code, and I don't know of anything faster.
    OK, let's share the patent.

    I don't know where some of you got the idea that I need a big array or file to
    store all the integer range for testing purpose. I actually generate a number
    at a time, format and confront it, and afterwhile I throw it away

    If I want to "see" them I display some thousands cases, or write them on a file,
    but this is not what I'm trying to do for the time being.

    The entire process [4 billions and change] takes 4-5 minutes on my pc, and it is quite fun to see how
    many things my pc can do in so short time.

    This test is useless, but it is feasible. My machine can easily handle it, and I'm enjoyng
    trying to do it in different shapes as well.

    There are far better ways to test this function, and probably the pre-test on
    particular cases I performed some days ago is enough. But what about the FUN?
    Why is it so difficult to get this point? Having fun is not a rational matter, it just
    depends on the attitude you have towards things, persons, situations...

    As far as I know, having fun is the best way to learn anything, as children teach us.

  2. #32
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    But what about the FUN?
    Why is it so difficult to get this point? Having fun is not a rational matter, it just
    depends on the attitude you have towards things, persons, situations...
    Are you trying to accuse me of something like ruining your fun? I appreciate the concern but it is unfounded. I am also very uninterested in what you find fun if this is truly an example.

    Anyway, next time you want to make a number into a string, consider using sprintf or something.

  3. #33
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by frktons View Post
    Well, this is a good point. I assumed the limits are already
    Actually I tested it and it takes about 1/3 second.
    I was referring to your digit permutations of various lengths up to that large number. If you can get that to run in 1/3 seconds, we will all sing Kumbaya and dance around you.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #34
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by frktons View Post
    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;
    }
    Screw the extra flags, there's a much simpler way to exit nested loops that doesn't incur extra overhead for every loop iteration:
    Code:
    for (x = 0; x < lim0; x++) {
        for (x1 = 0; x1 < lim1; x1++) {
            for (x2 = 0; x2 < lim2; x2++) {
                if (condition) {
                    x1 = lim1-1;
                    x = lim0-1;
                    break;
                }
            }
        }
    }
    It only adds overhead for when you actually want to break out (versus a goto).
    I'm in favour of putting such a thing in its own function and using a return though.

    As has become evident here though, just use a known correct function for building the strings. Even in the extremely unlikely case that the itoa function had a bug, it would be even more unlikely that it would be the same bug as other code. At the end of the day you have to look at how big your test is and make a judgement call as to whether it's easier to be sure that your test is correct with that huge thing you wrote, or with something smaller.
    I'm really pleased that you're enthusiastically getting into unit tests. I am also, and built my own mini framework for it.
    Last edited by iMalc; 07-19-2010 at 03:44 AM.
    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"

  5. #35
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by claudiu View Post
    I was referring to your digit permutations of various lengths up to that large number. If you can get that to run in 1/3 seconds, we will all sing Kumbaya and dance around you.
    Well! then start singing and dancing That is the time it takes.

    Quote Originally Posted by iMalc View Post
    Screw the extra flags, there's a much simpler way to exit nested loops that doesn't incur extra overhead for every loop iteration:
    Code:
    for (x = 0; x < lim0; x++) {
        for (x1 = 0; x1 < lim1; x1++) {
            for (x2 = 0; x2 < lim2; x2++) {
                if (condition) {
                    x1 = lim1-1;
                    x = lim0-1;
                    break;
                }
            }
        }
    }
    It only adds overhead for when you actually want to break out (versus a goto).
    I'm in favour of putting such a thing in its own function and using a return though.

    As has become evident here though, just use a known correct function for building the strings. Even in the extremely unlikely case that the itoa function had a bug, it would be even more unlikely that it would be the same bug as other code. At the end of the day you have to look at how big your test is and make a judgement call as to whether it's easier to be sure that your test is correct with that huge thing you wrote, or with something smaller.
    I'm really pleased that you're enthusiastically getting into unit tests. I am also, and built my own mini framework for it.
    Yes, Malcom, It is something I used myself some time ago trying
    to avoid the evil goto and fortunately it works just fine
    with a small overhead.

    The function/return solution is my preferite as well.

    I'm glad you took the time to read some posts and comment them.
    I really like your aptitude to code and dissect Algorithms

    The code I wrote to generate the strings looks a little bit oversized
    compared to the thing I'm trying to test. But it'll work just fine and
    fast. Well it already does, but I'm adding something extra and I did
    not get time enough to assemble, test and post it.

    It'll come shortly. Only for those who care, please.
    Last edited by frktons; 07-19-2010 at 04:40 AM.

  6. #36
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by frktons View Post
    Well! then start singing and dancing
    Well, post the code here and then I will gladly.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #37
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by claudiu View Post
    Well, post the code here and then I will gladly.
    Ok, I looked at the code I realized a week ago, and it is not 330 ms
    as I recalled, but 3,330 ms on my machine. Probably on yours it 'll vary
    depending on what compiler, system, CPU you are using.

    I attach the routine, if you test it on your machine, please post the results
    you get.
    Last edited by frktons; 07-19-2010 at 05:56 AM.

  8. #38
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ehhh... a few comments about your code.

    Firstly, yes I have tested it on my travel Dell Mini 10v netbook which granted may disadvantage you, since it is lower in specs than your average desktop. I got about 17.6 seconds for the whole thing, which to be honest is not impressive by any means.

    Secondly, you are falling in love with GOTO. Snap out of it! The main problem with using goto is that once you start using it, you think using it anywhere is fair game. It's not. While people advocate that it can be an optimum solution in some situations (and indeed it is), such cases should be extremely rare in your code. The rule of thumb is not to think whether you can use a goto, but think whether you can avoid it. If avoiding GOTO produces worse results, than you can go ahead and use it.... very sparingly though!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #39
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Nested loop does not always mean
    Code:
    for(i = 0; i < lim0;i++) {
     for(j = 0; j < lim1 ; j++) {
    }
    }

  10. #40
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by claudiu View Post
    Ehhh... a few comments about your code.

    Firstly, yes I have tested it on my travel Dell Mini 10v netbook which granted may disadvantage you, since it is lower in specs than your average desktop. I got about 17.6 seconds for the whole thing, which to be honest is not impressive by any means.

    Secondly, you are falling in love with GOTO. Snap out of it! The main problem with using goto is that once you start using it, you think using it anywhere is fair game. It's not. While people advocate that it can be an optimum solution in some situations (and indeed it is), such cases should be extremely rare in your code. The rule of thumb is not to think whether you can use a goto, but think whether you can avoid it. If avoiding GOTO produces worse results, than you can go ahead and use it.... very sparingly though!
    Good points. As your firmature says:
    "Success is the ability to go from failure to failure without losing your enthusiasm"
    so I carry on the hard job of learning C without losing my enthusiasm.

    Quote Originally Posted by Bayint Naung View Post
    Nested loop does not always mean
    Code:
    for(i = 0; i < lim0;i++) {
     for(j = 0; j < lim1 ; j++) {
    }
    }
    I guess you are right all the way long.

  11. #41
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    the second stage ===> Look Here is over, itoas() has been fully tested, and there you can find the
    source attached, with No goto at all.

    Enjoy

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