Java Reference
In-Depth Information
At this point, there are two suspended computations waiting to resume and the
computer begins to execute this new recursive call, which is handled just like all the
previous recursive calls. The argument 1 is substituted for the parameter n and the body
of the method is executed. At this point, the computation looks like the following:
if (123 < 10)
{
System.out.println(123);
}
else //n is two or more digits long:
{
writeVertical(123/10);
System.out.println(123%10);
}
if (12 < 10)
{
System.out.println(12);
}
else //n is two or more digits long:
{
writeVertical(12/10);
System.out.println(12%10);
}
if (1 < 10)
{
System.out.println(1);
}
else //n is two or more digits long:
{
writeVertical(1/10);
System.out.println(1%10);
}
No recursive
call this time.
When the body of the method is executed this time, something different happens.
Since 1 is less than 10 , the Boolean expression in the if-else statement is true , so the
statement before the else is executed. That statement is simply an output statement
that writes the argument 1 to the screen, so the call writeVertical(1) writes 1 to the
screen and ends without any recursive call.
When the call writeVertical(1) ends, the suspended computation that is wait-
ing for it to end resumes where that suspended computation left off, as shown by the
following:
output the
digit 1
if (123 < 10)
{
System.out.println(123);
}
else //n is two or more digits long:
{
writeVertical(123/10);
System.out.println(123%10);
}
if (12 < 10)
{
System.out.println(12);
}
else //n is two or more digits long:
{
writeVertical(12/10);
System.out.println(12%10);
}
Computation resumes
here.
Search WWH ::




Custom Search