Question on how this works?
public static void mystery(int num)
if (num > 0)
mystery(num – 1);
System.out.print(num + " ");
I was looking through my older notes from a class I took a few years ago, and it was saying the answer was 1 2 3 4 is what outputted when this is run. I would think it is 4 3 2 1, and I don't understand how it is the other way around. Would anyone be kind enough to explain what actually happens to make it 1 2 3 4? Thank you.
Re: Question on how this works?
The print is after the recursive invocation of mystery. The last deepest invocation is with 0 but since there is the condition num>0, no recursion and no print happens. Then gets one step back, where the invocation was with 1 and print it ... and go back where was 2 .....
Originally Posted by sammythesp3rmy