Why with number 123 are the two last lines executed after 'return' and not with number '1'
This is a recursive method. Don't think of it as one method. When the number is 123, these are three methods.
After the return is executed, as we know, the code after the return statement in this method will not be executed, but this will not affect other methods.
In this example, return is executed only once in the last method. In the first two methods, the code does not enter the if block.
It might be easier to understand to translate it into a normal method, it's like(when number is 123):
static void cijfers(int n)
{
cijfers1(n / 10);
Console.Write(" {0} ", n % 10);
}
static void cijfers1(int n)
{
cijfers2(n / 10);
Console.Write(" {0} ", n % 10);
}
static void cijfers2(int n)
{
Console.Write(" {0} ", n);
return;
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.