Recursive Method Prints 4 Times

后端 未结 4 650
没有蜡笔的小新
没有蜡笔的小新 2021-01-17 04:05

So I am working on learning how to utilize recursion through Java. I have written a simple program that adds all the numbers between 1 and n and it looks to do it\'s job. Wh

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-17 04:42

    When the recursion is stack is full, then it starts emptying itself from the tail. This is the reason your print statement below the if statements starts to get executed. Note: Recursion process will return its value where its called, so if conditions arent satisfied so it will return to that point, but the whole recursion process will keep on continuing until the stack is emptied. Whatever i said is what is called the Tail Recursion.

提交回复
热议问题