Thread: Breaking a recursive cycle!

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    Breaking a recursive cycle!

    Any ideea on how to do that? Something like break for a loop.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A recursive function should only be broken when the base condition is met. So in your function you need to have an if test.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could just stop recursing, and the stack would unwind.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Yea use a loop with a break statement.

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Code:
     
     
    void recursiveFunction(arguments){
     
    if(conditionMet)
    return;
     
    --- do something----
    recursiveFunction(arguments);
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    You can also use longjmp.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    longjump ? what's that ?

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    It's like a global goto. I suggested it just as a joke, you shouldn't really use it unless you have to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  2. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  3. Algorithm help (Changing from Recursive to Non Recursive)
    By Thantos in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 07:27 PM
  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. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM