How is this recursion cycle run

I have a line of code in which I cannot understand how the codes are executed, I mean the program flow.

code:

1) public class RecurLoopTest { 2) public static void main(String[] args) { 3) printit(2); 4) } 5) private static int printit(int n){ 6) if(n>0){ 7) printit(--n); 8) } 9) System.out.print(n+","); 10) return n; 11) } 12) } 

I think the output should be: 0,

But the conclusion: 0,0,1,

I have done DEBUG above the class so many times, The thread I saw when debugging:

  • It starts execution from line 3, that is, it calls printit () with parameter 2. as a parameter.
  • line 6, if the condition checks the value of n , if it is greater than zero, then the controls go to line 7, i.e. prinit () calls the decrement value n again.
  • step 2 continuous execution until the value n becomes 0, then line 6, if the condition is false. therefore, on line 9, SYSO prints 0.
  • Then I couldn’t understand how the control again goes from line 10 to 5?
+6
source share
4 answers

In this program, printit() is called three times.

how does control move again from line 10 to 5?

Here's how the thread controls:

  • printit(0) - n=0 , prints "0," , returns 0
  • printit(1) - n=1 , prints "0," , returns 0
  • printit(2) - n=2 , prints "1," , returns 1
  • main()

Conclusion: 0,0,1, For a better understanding, put a breakpoint in the return statement during debugging.

Here is the control / memory circuit: enter image description here

EDIT 1: Alternatively, the image below explains: enter image description here

+4
source

Let me explain you step by step using debug mode.

Look at the stack of methods at the top in debug mode.

Since this is a recursive method that is called itself up to n>0 . This means that there are three calls to this method, as shown in the snapshot below.

enter image description here

Now it will return the value and return from the method stack, as shown below.

enter image description here

enter image description here

Finally, it will return to the main method, from which it is called

enter image description here

Since this method is called three times, you get 0,0,1,

+3
source

On the first call, n is 2,

 if(n>0){ // 2, note this is an if - not a while loop. printit(--n); // recurses, after this n = 1; references won't update by the recursion. // Well, not with primitives or immutable types anyway. // n = printit(n - 1); // <-- This would behave like you seem to expect. // n = printit(--n); // <-- and so would this, but not in C! } System.out.print(n+","); // prints 1 !!!! return n; // <- returns 1 
+2
source

Do not think about calling a recursive method, but first call the method. If you call a method mainly, for example, program execution jumps to this method and executes all the code inside this method. After the method is executed, the program flow returns to the main one and continues to execute lines of code basically. The same goes for a recursive call.

In the first run using 2, 2 is greater than 0. Thus, you decrease 2 to 1, and then call printIt(1) . This means that the program will return here after completion of the function. But since printIt () is recursive, the next call also expects any method calls within itself.

So, the next time you call n, now 1, which is greater than 0. You reduce it to 0, then call printIt(0) . When this event is executed, it completes the if (n > 0) check and goes to line 9 to print 0 and then returns.

Since this is completed, the program returns to the second call to printIt() . Remember that in this call n is 0. Now he can go to his own line 9 and print 0. Then he repeats the initial call to printIt() , which prints 1.

So, when everything is said and done, you get 0,0,1.

0
source

All Articles