Can you do Recursion in your head? if so, how do you do it?
Code:
static void P(int x)
{
if (x > 0)
{
Console.WriteLine(":" + x);
P(--x);
}
if (x > 0)
{
Console.WriteLine("::" + x);
P(--x);
}
Console.WriteLine(":::" + x);
}
static void Main(string[] args)
{
P(3);
Console.ReadKey();
}
Above you will see some code I was contemplating today while I was driving on the highway. I was able to predict the first 6 lines of output
Code:
:3
:2
:1
:::0
:::0
::1
but after the 6th line I came up with a bunch of nonsense because I reached a point where I couldn't mentally hold the flow of the algorithm.
If you look at the above code, are you able to mentally correctly come up with the entire output?
If so, how do you do it? Do you picture like a spiral around each recursive call?