Thread: Return from within loop?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Return from within loop?

    Hi,

    Is returning from within a loop ok?
    The question may have been asked before.
    I tried searching for "loop return", but got a forum error:
    Fatal error: Allowed memory size of 33554432 bytes exhausted ...

    Code:
    #include <stdio.h>
    
    void func_a(void)
    {
      int i=0;
      while(i++<10)
      {
        printf("i=%d\n",i);
        if(i==5)
          return;  /*   return from within a loop, possible problem or not?    */
      }
    }
    
    int main(void)
    {
      func_a();
      return(0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It certainly is okay, and your code does not appear to have any problems that should result in the error that you mentioned.
    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

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    I think the OP is referring to the forum search feature, I too got that error a half hour ago.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh yes, I see now.
    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

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    As long as you release any local dynamic memory or other resources before you exit, yes it's fine.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Also note these:
    1) return. You get out of the loop and the function
    2) break. You get out of the loop and the function continues normally after the loop
    3) goto. Not recommended, but an option

    Also, you can use continue to get to directly to the next iteration inside the loop.

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    This is probably (in my opinion at least) the best way to break out of nested loops. I use this all the time.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    Thanks everybody. I am good to go

  9. #9
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Also note that if you do this in a non-void function, you will probably get a warning unless you have another return as the last line of the function as well.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM