Consider this method:
Code:
void myfun( int cntr)
{
if(cntr == 0)
return;
else
{
System.out.println("the counter is " + cntr);
myMethod(--cntr);
System.out.println(""+cntr);
return;
}
}
If the function is called with the value 4, have you any idea what will the result be ? The above shown recursion is basically a loop similar to a for loop or a while loop. When do we favor recursion to an iterative loop? We make use of recursion when we observe that our issue can be made simpler or simplify to a certain extent can be solved after further decrease.
Bookmarks