It performs much the way a single recursion method does. Let's break this down:
if(x>0) //x == 3
{
System.out.println(x + "top");
f(x-1); //Passes in 2
//---------- Break 1
System.out.println(x + "bottom");
f(x-1);
}
It hits the recursive call and goes to:
if(x>0) //x == 2
{
System.out.println(x + "top");
f(x-1); //passes in 1
//------- Break 2
System.out.println(x + "bottom");
f(x-1);
}
And again:
if(x>0) //x == 1
{
System.out.println(x + "top");
f(x-1); //Passes in 0
//----------- Break 3
System.out.println(x + "bottom");
f(x-1);
}
Then in the next recursive call the base case is hit so it does not enter the if and prints bert
and then exits the method.
Then the recursive call goes back to caller, which is is at break 3:
if(x>0) //x == 1
{
System.out.println(x + "top");
f(x-1); //Passes in 0
//----------- Break 3
System.out.println(x + "bottom");
f(x-1);
//---------- Break 4
}
So it then hits the second recursive call and passes in 0
. This will hit the base case and stop the recursion.
Then the method will return to the caller, which is at break 4. This ends that method, so it returns to the caller of that method, which is break 2.
if(x>0) //x == 2
{
System.out.println(x + "top");
f(x-1); //passes in 1
//------- Break 2
System.out.println(x + "bottom");
f(x-1);
}
Then the second recursive call will be hit and pass in x == 1
, which is the same case as above, so I won't repeat it. Then this returns to the method that called it, which is break 1. Then that method calls the second recursive call, which is x == 2
, which again is a repeat.