The first thing you do in the recursion is print the element, and then recursively call the function.
Let's have a look on the call stack of the recursion. [we will denote each recursive call by its argument a]
At first, you only invoke the method with one element
| |
|1|
---
you will print "1" and invoke recursive call with 2, now the top will be:
|2|
|1|
---
You enter the method again and print the element, 2, and reinvoke with 3, and get:
|3|
|2|
|1|
---
continue this logic, and you will end up printing 1,2,3,4, and get the stack trace:
|5|
|4|
|3|
|2|
|1|
---
Now, the condition does not meet, so you only print fin, and return - results in popping the first element:
|4|
|3|
|2|
|1|
---
when you are back from the recursive call, you print the top+1 [5] and then fin, and again, pop the element from the head of the stack trace, and get:
|3|
|2|
|1|
---
Now again, you print the head+1: 4 and fin, and pop one more element....
Continue this logic until the stack is empty, and you get exactly what the program printed!
a>=5. The recursion is invoked only ifa<5, thus!(a<5) == a>=5terminates it.