What is the difference between iteration and recursion?

后端 未结 8 869
太阳男子
太阳男子 2021-01-30 11:52

What is the difference between iteration and recursion and why/when is one better:

while (true) {
    // Iterating
}

8条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 12:15

    They can be used interchangeable to solve different problems. In essence you can write recursive functions iteratively and vise versa.

    Iteration may increase the performance of your program. Whereas recursion may give a more intuitive and elegant result. You can choose either which to your preference!

提交回复
热议问题