# Recursion v. Iteration

I’m an aspiring Software Developer ..I have to learn to drop the aspiring part. After 3+ months and 1200+ hours learning Java I should be able to confidently say, without a doubt, that I am a Full-Stack Developer ..but it’s hard.

Job searching is an arduous and redundant exercise in exhaustion that can honestly lead you into wanting to cut off all ties and truly consider the hermit lifestyle(Would I be able to live without my cellphone?!). I’ve had several interviews over the past two months where the communication was excellent, and my ability to talk about myself, tech, my projects and answer basic to intermediate OOP questions were great but I found myself struggling with whiteboard equations and assessments. With programming, failing early and often is key! This showed me that I need to become a more solid coder.

In the process of bettering myself as a coder, I’m going to write weekly blogs with solutions for algorithms you may see on an coding assessment or during a whiteboard solution. I’ll solve the equation in 2 or more different ways and explain how they work and my preference, I appreciate and welcome any feedback!

Today we’re working with the Fibonacci sequence and I solved it with two different methods. One using recursion, and the other, iteration:

**Recursion:**

public static int fibonacciRecursion(int nthNumber) {

if(nthNumber == 1 || nthNumber == 2) {

return 1;

}

return fibonacciRecursion(nthNumber-1) + fibonacciRecursion(nthNumber -2);

}

**Iteration:**

public static int fibonacciLoop(int nthNumber) {

int temp1, temp2, result = 1;

if(nthNumber == 1 || nthNumber == 2) {

return 1;

}

for(int i = 3; i <= nthNumber; i++) {

result = temp1 + temp2;

temp1 = temp2;

temp2 = result;

}

return result;

}

Honestly, I prefer the look of recursion over iteration. It’s streamlined, the code is clean, concise, and easier to read. Although, if we’re looking at it in terms of memory usage, iteration is definitely the winner.

Recursion, by definition, is the repeated application of a recursive (characterized by recurrence or repetition, in particular)procedure or definition. That constant repeated application takes a lot of time, longer than if simply iterated through with our second example. Iteration may look a bit clunky but it’s way more efficient and probably the best way to go as an early coder.

TL;DR: it’s basically up to the user! of course there are pros and cons, and you’ll have situations where memory is really short and not a second can be spared within an application and recursion simply can’t be an option. Other than that, the choice is yours!