XDProgrammer
XDProgrammer

Reputation: 861

Printing Recursively

I have this class the just prints an int:

class PRecursion  {
    public static void main(String[] args){     
       foo(1);
 }

public static void foo(int x){
    System.out.println("" + x);

    if (x < 3)
        foo(x+1);
    System.out.println(""+x);   
   }

}

Output:

1
2
3
3
2
1

Why is it the is printing backwards then decrements?

Upvotes: 0

Views: 1806

Answers (3)

Kugelblitz
Kugelblitz

Reputation: 582

It is not actually decrementing. What is happening is:

  • You are calling foo(1).
  • foo(1) does it's thing and starts a recursion.
  • Recursion goes on for how long you tell it to (foo(3) in your example)
  • After the recursion is done, foo(1) still has one statement to execute, and that is:

    System.out.println(""+x);

hence you are seeing the print of foo(1) a second time, but after all other prints have been made. The same goes for every other time foo() is being called with a different number. So in the end it looks like it is decrementing, when really you are only printing a second time for the same value, in reverse order.

Upvotes: 4

singhakash
singhakash

Reputation: 7919

The value of x is not decrementing but it executes the remaining part of code which looks like decrementing.

  System.out.println(" " + x);   // 1 2 3

    if (x < 3)
        foo(x+1);
  System.out.println(""+x);   //3 2 1

due to recursion it calls the same function again and again and print only the first print statement until the if condition is true but at the end when if is false it stop executing foo(x+1) and exute the remaining print statements as the execution in the last step x is 3 so it prints 3 2 1 .

Maybe this could clear a bit more


Execution flow


Since all the foo(1)-->foo(2)-->foo(3) is executed causing output 1 2 3 the execution is in the last method foo(3) so it started executing the remaining method statement with flow like foo(3)-->foo(2)-->foo(1) printing the remaining print statement which executes looks like all the function backwards resulting in remaining 3 2 1.

Upvotes: 3

Persixty
Persixty

Reputation: 8579

Try this:

class PRecursion  {
    public static void main(String[] args){     
       foo(1);
 }

public static void foo(int x){
    System.out.println("before " + x);

    if (x < 3)
        foo(x+1);
    System.out.println("after "+x);   
   }

}

The second print statement doesn't decrement the input. What is happening is it leaves the call of foo(3) before it leaves the call of foo(2) and then finally `foo(1). So it executes the second print statements in the reverse of the order of the first.

If you read out the execution of the program as:

set x=1
print x (i.e. 1)
push x=1 on stack and set x=2 (this is how you can read foo(x+1))
print x (i.e. 2)
push 2 on stack and set x=3
print x (i.e. 3)
Don't push anything (x<3 is false)
print x again (i.e. 3)
pop x=2 off the stack
print x (i.e. 2)
pop x=1 of the stack
print x (i.e. 1)

stripping out the pushing and popping you get:

print 1
print 2
print 3
print 3
print 2
print 1

Upvotes: 3

Related Questions