Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Recursive: See recursive
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01083365
Message ID:
01083780
Vues:
9
>Rick,
>I don't see how I can accomplish what I want without the bool return value.
>Your example will always recurse through all nodes in the tree (please correct me if I am wrong because I find recursion very mind bogling).

The problem I see is that you're using the return value of the recursive method as a flag that determines whether the recursion should continue. Lets put aside the boolean value for a moment.

Lets go back to your original question: What is the best way to "break-out" of the recursion when my condition is true?

The short answer is that the best way to breakout is to just stop calling the recursive function when the condition is met.

Usually recursive methods return a value (you're usually looking for or calculating something). The classic example of this is a method that calculates the factorial of a number. I'm sure you know what a factorial is, but for the sake of other readers, the factorial of a number is the product of all positive integers less than or equal to the integer.

So for example the factorial of 5 (usually written 5!) is
5*4*3*2*1

Now you could calculate this with a loop
private int factorial (int nValue)
{ 
   int iReturnValue =1;

   for(int n=nValue;n>1;n--)
      iReturnValue *=n;
   
   return iReturnValue
}
but another way is to use recursion:
private int factorial(int nValue)
{
   if(nValue==1)
      return 1;
   
   return nValue*factorial(nValue-1);
}
When the condition is met (nValue==1) a concrete value is returned instead of returning the result of the a recursive call.

If you're still confused, maybe you could describe the problem you are trying to solve?

Hope this helps,
Rick
Rick Hodder
MCP Visual Foxpro
C#, VB.NET Developer
Independent Consultant
www.RickHodder.com
MyBlog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform