iterative versus recursion

261 viewsOther

we use this in college because our program is Information Systems. i still do not get it.

In: Other

7 Answers

Anonymous 0 Comments

In very general terms, think of it as building up to a solution versus working your way down to one.

The classic example in programming for recursion is the factorial function. A! = A x (A-1) x (A-2) x … x 1.

An iterative solution would start with one and work up. X factorial is 1, times 2, times 3… up to X. “Do the multiplying thing X times” is the iteration.

A recursive solution starts from the top and works down to an end-case. Here, it’s 1! = 1. So what is X factorial? It’s X times (X-1) factorial. What’s (X-1) factorial? It’s (X-1) times (X-2) factorial… until you get to 1. “Call this same function with one less than before” is the recursion.

You are viewing 1 out of 7 answers, click here to view all answers.