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);
}
}
Sachin SinghPosted Jun 20, 2021, 4:53 AM
returnstatement terminates execution of the method in which it appears and returns control to the calling method, so according to you it should return the flow to the Main method, isn't it? But why is it not returning the flow to the Main method, this is your question, to understand it read the last point?Valerie MeunierPosted Jun 20, 2021, 8:26 AM
Valerie MeunierPosted Jun 19, 2021, 8:21 PM
Valerie MeunierPosted Jun 19, 2021, 3:05 PM
Sachin SinghPosted Jun 19, 2021, 1:38 PM