Valerie Meunier

Valerie Meunier

  • 824
  • 693
  • 72.2k

how can this line be executed?

Jun 19 2021 12:58 PM

Hi

i don't understand how the last line (in bold) can be executed. The method is called just before that line, so in my eyes, this line will never be executed, because (suppose the entered value = 12) when the method will start for the second time with value n/10, it will start from above and be stopped by the IF statement, no? But nevertheless it is executed. Thanks to explain me the logic order. When does that line be reached?

using System;
public class x4
{
    static void Main()
    {
    Console.Write(" Input any number : ");
    int num = Convert.ToInt32(Console.ReadLine()); 
    Console.Write(" The digits in the number {0} are : ",num);
    separateDigits(num);
    Console.Write("\n\n");
    }

    static void separateDigits(int n)
    {
        if (n < 10)
        {
            Console.Write("{0}  ", n);
            return;
        }
        separateDigits(n / 10);
        Console.Write(" {0} ", n % 10);
    }
}

 


Answers (5)