I thought it beneficial to capture this super simple example of recursion I tweeted because it’s usually scary, obtuse, or part of a larger topic.

countdown(int i) { print(i); if (i > 0) countdown(i - 1) }

This means countdown(5) would print the following: “5”, “4”, “3”, “2”, “1”

The alternative would be a loop like this.

countdown(int i) { while (i > 0) { print(i); i -= 1 } }

Recursion is great at simplifying logic that traverses structures (the classic example being graphs) or accumulates values (the classic example being the Fibonacci sequence)

#ProgrammingTips