6

I have some line of code where i cannot understand how the codes are executes, i mean the flow of program.

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 am thinking output should be: 0,

But Output is: 0,0,1,

I have done DEBUG above class so many time, The flow i have seen while debugging:

  1. Starts execution from line 3, i.e. invokes printit() with 2 as parameter.
  2. line 6, if condition checks n value if greater than zero, then controls goes to line 7, i.e. prinit() invokes again by decrements n value once.
  3. step 2 continuous execution until n value becomes 0, then line 6, if condition is false. so in line 9, SYSO prints 0.
  4. Then, unable to understood that, how the control again goes from line 10 to 5 ??
3
  • try following the value of 'n' in each function using your debugger. Commented Apr 13, 2014 at 4:09
  • 1
    The one thing you are missing is this: After the recursive call to printit() in line 7 is finished, the rest of the function executes. Each invocation of printit() prints output after the recursive call is complete, so it essentially does three print-outs "on the way out" of the recursion. This is why the output is a set of three values, not just a single value like you said you were expecting. Commented Apr 13, 2014 at 4:14
  • You are calling printit() on n-1 and printing n-1 afterwards. Commented Apr 13, 2014 at 4:15

4 Answers 4

4

In this program printit() invoked three times.

how the control again goes from line 10 to 5 ??

Here is how the control flows:

  • 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()

So Output is: 0,0,1, To understand better put a breakpoint in return statement while debuging.

Here is control/memory flow diagram: enter image description here

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

Sign up to request clarification or add additional context in comments.

Comments

3

Let me explain you step by step using debug mode.

Please look at the stack of methods bottom to top in debug mode.

Since its a recursive method that is called by itself until n>0. It means there are three calls of this method as shown in below snapshot

enter image description here

Now it will return the value and come back from the stack of methods as shown in below snapshots

enter image description here

enter image description here

Finally it will return back to main method from where it is called

enter image description here

Since this method is called three time hence you are getting 0,0,1,.

Comments

2

On the first invocation 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 Comments

please elaborate more about this line: return n; // <- returns 1, how n value is 1?
Because 2 - 1 is one. The thread of execution returned from the recursion, now if you had n = printit(--n); then you would have gotten your expected output.
0

Don't think about a recursive method call, but any method call first. If you call a method in main for example, the program execution goes to that method and executes all of the code within that method. Once the method is done executing, the program flow returns to main and continues executing the lines of code in main. The same applies to a recursive call.

On the first run using 2, 2 is greater than 0. So you decrement 2 to 1 and then call printIt(1). This means the program will return here once the function it calls is finished running. But since printIt() is recursive, the next call also waits for any method calls within itself.

So on the next call, n is now 1, which is greater than 0. You decrement it to 0, then call printIt(0). When this occurrence runs though, it fails the if (n > 0) check and proceeds to line 9 to print 0 and then returns.

Since that is complete, the program returns to the second call of printIt(). Remember that in this call, n was 0. It can now proceed to its own line 9 and print 0. This then repeats to the original call to printIt() who prints 1.

So when all said and done, you get 0,0,1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.