Java Reference
In-Depth Information
The execution is suspended before the
System.out.println
. On the next
recursion,
n = 2
; the code to be executed is
if
(2 >= 1)
{
writeUp(1);
System.out.print(2 + " ");
}
The execution is suspended before the
System.out.println
. On the next
recursion,
n = 1
and the code to be executed is
if
(1 >= 1)
{
writeUp(0);
System.out.print(1 + " ");
}
The execution is suspended before the
System.out.println
. On the final
recursion,
n = 0
and the code to be executed is
if
(0 >= 1)
// condition false, body skipped
{
// skipped
}
The suspended computations are completed from the most recent to the least
recent. The output is
1 2 3
.
11.
Trace for Self-Test Exercise 5: If
n = 3
, the code to be executed is
if
(3 >= 1)
{
System.out.print(3 + " ");
writeDown(2);
}
Next recursion,
n = 2
, the code to be executed is
if
(2 >= 1)
{
System.out.print(2 + " ");
writeDown(1)
}
Next recursion,
n = 1
, the code to be executed is
if
(1 >= 1)
{
System.out.print(1 + " ");
writeDown(0)
}