DEV Community

Tatiana Bowman
Tatiana Bowman

Posted on

If you can use a while loop you can use recursion (coming from a ex recursion hater)

I bet you've heard the phrase "If you can do it recursively you can do it iteratively" true but that still didn't make me hate it any less when I first started learning it. Recursion was a huge pain but once you get the pattern down even a little it's easier and becomes a very useful tool. One thing that helped me was realizing that while loops (which I love) and recursion are basically the same thing just slightly different code.

*What is recursion? *

Recursion is the act of a function calling itself within itself. When solving with recursion you need two things 1. a base case which is used as a sort of stopper for the function and 2. a recursive call which is the act of the function calling itself.

Image description

Image description

In the example above I started with a base case that states if the number is less than or equal to zero print "Happy New Year", so when the number gets down to zero it will print the message.

The way to get it to zero is through the recursive call on line 9, as you can see every time I call the function it decrements the number by one getting it closer and closer to the base case.

So you call the function it takes the number and checks if it's equal to zero if it isn't it'll print, decrement by one then get called again but as a smaller number each time.

**

What is a while loop?

**
A while loop creates a loop that executes a statement as long as the condition evaluates to true. A while loop can be used just like a for loop, having the exact same syntax but it can also be used like recursion.

Image description

In the above example, I started with a condition that states while the num is more than 0, print the num then decrements so once it hits 0 it'll break out of the loop and print "Happy New Year!!".

As you can see the base case of the recursive solution and the while loop condition are basically the same the only difference is the way it's stated. The base case says "if the number is less than or equal to zero". And the while loop says " While the number is more than zero". Both need to hit zero in order for the breakout and both have to decrease the number to get there.

**

side by side:

**

Image description

**

Here's a more complex example of how they can be used w/ pseudo code to help break it down:

**

Image description

Image description

**

Conclusion

**

I hope this comparison helped to give you a better understanding of recursion and how it can be used. Recursion is tricky to learn but it can allow you to break down big problems into smaller ones, and can sometimes even produce simpler solutions. If it helps try and think of it like a yoyo.

Because this post is more of a comparison of while loops and recursion than explaining recursion, If you want to learn more about it here are a few links that really helped me:

What is recursion - In Depth
https://youtu.be/6oDQaB2one8

Recursion Crash Course:
https://youtu.be/lMBVwYrmFZQ

Stepping through recursion Fibonacci function:
https://www.youtube.com/watch?v=zg-ddPbzcKM

(P.S. I still kinda hate it!)

Top comments (0)