Do not think about calling a recursive method, but first call the method. If you call a method mainly, for example, program execution jumps to this method and executes all the code inside this method. After the method is executed, the program flow returns to the main one and continues to execute lines of code basically. The same goes for a recursive call.
In the first run using 2, 2 is greater than 0. Thus, you decrease 2 to 1, and then call printIt(1) . This means that the program will return here after completion of the function. But since printIt () is recursive, the next call also expects any method calls within itself.
So, the next time you call n, now 1, which is greater than 0. You reduce it to 0, then call printIt(0) . When this event is executed, it completes the if (n > 0) check and goes to line 9 to print 0 and then returns.
Since this is completed, the program returns to the second call to printIt() . Remember that in this call n is 0. Now he can go to his own line 9 and print 0. Then he repeats the initial call to printIt() , which prints 1.
So, when everything is said and done, you get 0,0,1.
source share