Back to questions
JuniorTheoryOccasional
What does the program print?
Count(3);
void Count(int n)
{
if (n > 0)
{
Console.Write(n);
Count(n - 1);
Console.Write(n);
}
}321123
- Expecting
321: the line after the recursive call still runs once that call returns, so eachCountprints itsntwice. - Expecting
321321: the deepest call finishes first, so the second prints come back in reverse order:1,2,3.
- What does the program print if the call is
Count(0)instead ofCount(3)? - What happens if the condition
n > 0is removed, and can the program recover from it?