If no base (or ending) condition is hit, then the recursion does not stop; this is called infinite recursion.
Here is an example:
public void mystery (int x)
{
if (x <= 0)
//the base or ending condition
return; //a void method can simply end by executing return
else {
System.out.print( x + " "); //notice it is print
so cursor stays on same line
mystery( x - 2);
//recursion the method is calling itself
}
}
What is printed when mystery(4) is executed?
How to solve ..
develop a method trace table...
mystery(4)
prints 4 and a space then calls
[output: 4 ]
mystery(2)
prints 2 and a space then calls [output: 4
2 ]
mystery(0)
base condition of
if (x <= 0) is true: recursion stops
output & answer is:
4 2