Hi everyone,

We all face dificullity in coding recursivly, maybe most of new codders and CS students. However, my question today is related to How many number of execution been made by the recursive method. Which I think a bit tidious.

I'll be updaing this article with new questions any time I face a new problem.

Anyway, @Tanyawin suggest, is to learn scheme languge since everything there made by recursion. (loops,etc) But I think it needs some times which I don't currently have since we're hitting the exams door soon.

**My question is: when you invoke a recursive call inside the while loop how many times it actually executing. I don't seem to be able to visualize it.
**
e.g.

while(i < m(k-1)){
     //anything here
     sum = sum + 2;
     }

I do see a k-1 times calls but what happens next? will each one of the calls do the same thing.... can't visialize it.

will continue adding more question later on...

Thanks guys!

this what you have here would actually diverge. you need to make some changes to either i, k, or both to make sure that i < m(k - 1) would eventually turn false (if it already is true) so that you get out of your while loop.

anyways, you can define a separate variable somewhere in your code, name it count. insert a System.out.println(count); and a count++; statement at the end of your loop. make sure you initialize your count variable with 1. this would let you see how many times your loop is executing. you can do this many ways, but this is one.

Oh yeah sorry. i totally forgot the base case. Unfortunatenky, i cannot edit my question,, i hope any mode do it dor me.

I understand I can use a counter and see how many, but I cant visualize it. I dont want to get use or memorize the number of execution. I think it is a bad habit. Thanks @Bibiki

I'm not sure if this will answer your question, but here goes. As far as visualizing recursion goes look at it like those russian dolls that all fit inside of each other. Each time you call the function, it opens a new "doll" and executes the body until it either matches your base case or hits yet another recursive call. How many times it runs is dependent on the function(opening yet another "doll) and think of the smallest doll that doesn't open as your base case. let's say,for example, you have a recursive factorial function, in which you were to ask for the factorial of a number for as long as it is greater than zero. If you call this function on the number 5, it will run six times (5, 4, 3, 2, 1, and then it runs with zero, matches the base case, and ends.) Likewise, whether or not each run is the same is dependent on the body of your function. For example, lets say you have this code as the body of your recursive function (in this case, "n" is your parameter, and func is your function name, the statements involving otherVar are irrelevant they are merely examples)

if(n%2==0)
{
    otherVar*=2;
    func(n-1);
}
else if(n%2==1)
{
    otherVar+=1;
    func(n-1);
}
else
{
    return 1;
}

each new call would be different because n%2 would change each time. If you have no if/else or case/switch statements then each execution will be the same. Sorry for the length, I hope this helps you! Please, if you have any questions or if any of this is unclear just ask.