Recursion Output Uncertainty

Ok, so I'm just learning recursion, and I'm confused by one point. This is the code

public class RecursiveDemo { public static void showRecursion (int num) { System.out.println("Entering method. num = " + num); if (num > 1) { showRecursion(num - 1); } System.out.println("Leaving method. num = " + num); } public static void main(String[] args){ showRecursion(2); } } 

The output I get is:

 Entering method. num = 2 Entering method. num = 1 Leaving method. num = 1 Leaving method. num = 2 

My question is why I get the output " Leaving method. num = 2 ". Shouldn't I stop at the " Leaving method. num = 1 " since the number has already reached 1?

+7
source share
8 answers

Once the original call to this method leaves the if statement, it goes to System.out.println("Leaving method. num = " + num); . Since you called the message initially with a value of 2 , 2 is the num value for this part of the code.

Your code works as follows (pseudocode):

 Start First Call if statement Start Second call Skips if statement Print from Second Call End of Second Call End of if Statement Print From First Call End of First Call 

It looks like you have a fundamental misunderstanding of recursion. When you call your method using (num-1) as arguments, the parent call (the first call in this case) stores the num value as the argument, which in this case is 2 .

+7
source

So let's comment on the line below.

 //showRecursion(num - 1); 

What would you get? It should be

  Entering method. num = 2 Leaving method. num = 2 

And if you uncomment the line above. You should get the one you had.

+6
source

Not.

main will call showRecursion(2) , which in turn will call showRecursion(1) (so you get two "Enter" messages). At this point, the condition will not be met, so there will be no more recursion. So, now the program simply starts returning from each function call in turn, printing both β€œLeave” messages.

+4
source

This is because the initial call to showRecursion(2) is not yet complete.

+3
source

Consider the following:

 public static void showFirstRecursion (int num) { System.out.println("Entering method. num = " + num); if (num > 1) { showSecondRecursion(num - 1); } System.out.println("Leaving method. num = " + num); } public static void showSecondRecursion (int num) { System.out.println("Entering method. num = " + num); if (num > 1) { showThirdRecursion(num - 1); } System.out.println("Leaving method. num = " + num); } // I won't bother showing an implementation for showThirdRecursion, because it won't be called. public static void main(String[] args){ showFirstRecursion(2); } 

No problem, right? You expect to see the first method entered, the second one introduced (the third is not entered because num == 0), the second on the left, the first on the left.

There is nothing special about recursion. It simply calls a function call that occurs to call the function from which the call is part. A recursive call behaves conceptually in every way, like any other function call. The trick is the design of a recursive algorithm, i.e. The reason you want to call the same function that you are already in.

+3
source

Other answers already cover a specific question, but here is some information about using the debugger. This tutorial is for Eclipse, but pretty much tells you what you need to know for any visual debugger.

The basics are pretty simple, and it would be helpful to spend your time at least how to get through the code. The debugger is an invaluable tool for quickly checking the logic of your program and is much simpler than scattering print statements. A.

+1
source

Try "showRecursion (5);".

[Answer: This is recursion. There is more than one copy of the variable "num" in memory. "num" is a parameter; this is not a field, but it is not static.]

+1
source

So I realized that every time the method is called, the stack becomes full, i.e. 2.1, but when i> 1 does not match, it returns / aborts the call, and control is transferred to the line system.out.println, which prints the value starting from the top of the stack, i.e. 1,2

0
source

All Articles