I was wondering guys if you could explain me how RECURSION works inside a loop!? I got lost here so please anything we ll be much appreciated.

 public void perm(int[] list, int k, int m)
                {
                    int i;
                    if (k == m)
                    {
                        for (i = 0; i <= m; i++)
                            Console.Write(list[i]);
                        Console.WriteLine(" ");
                    }
                    else
                        for (i = k; i <= m; i++)
                        {
                            swap(ref list[k], ref list[i]);
                            perm(list, k + 1, m);
                            swap(ref list[k], ref list[i]);
                        }
                }

Recommended Answers

All 7 Replies

it would be better if you could give some hint on what do you want to achieve with this code and how you call it. it seems that nothing is wrong with your code

It s not my code , i just want someone to explain it to me. How does the recursion behave in a loop because when tracing it then it just doesnt fit. So Please if anyone could explain it to me. I understand recursion without loops perfectly but i got stuck with this one.

The main thing to remember is that when perm is called inside your loop, all new variables are created for the method.

hmmm, i don t understand quite. The main thing that bothers me is when the compiler gets to the recursion code inside the for loop does it leave the for loop and then again go until the base case is met and does the for loop increase or does it stay the same

Another important thing to remember is that a recursive method must have a stopping condition here it is k==m. The method prints something and "bubbles up" to the calling method etc.

Basically what happens is you end up with several copies of the function operating until the last one ends then they back up until the first one ends. so the for loops don't end when the new function is called, their execution is basically suspended until the call is returned.

function a
    calls new function a
        calls new function a
            calls new function a

from this simple structure you can see that each one that is called is an independant copy of the original. This bring us to one important thing to keep in mind. Each one that is called uses up more resources. If you run too many at the same time the whole thing will crash, expecially in this case where you're passing the parameters by value which creates separate variables for each routine that is called.

to make it simple, why don't you give this code a try? add a break on the line
int i;
by pressing f9 on the line. then call it (you can call it from button click event or from sub main, or even from immediate panel), when the code break, just press f8 to step forward. you can add watch to see how the variables changing value. it's all in visual studio function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.