Java Reference
In-Depth Information
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. The 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)
}
Final recursion,
n = 0
, and the
if
statement does nothing, ending the recursive calls:
if
(0 >= 1)
// condition false
{
// this clause is skipped
}
The output is
3 2 1
.
12.
6
13. The output is
24
. The method
rose
is the factorial method, usually written as
n!
and defined as follows:
n!
is equal to
n
*(
n
-
1
)
*
(
n
-
2
)*…*
1