Upcoming SlideShare
×

# Recursion and looping

4,163 views

Published on

A short comparison of iteration and recursion using example of factorial program.

1 Comment
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• good slide,thanks , xcoolanurag

Are you sure you want to  Yes  No
Views
Total views
4,163
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
110
1
Likes
2
Embeds 0
No embeds

No notes for slide

### Recursion and looping

1. 1. Iteration And recursion <ul><ul><li>Which is better? </li></ul></ul>
2. 2. Loops: <ul><ul><li>All languages need an iteration statement. </li></ul></ul><ul><ul><li>The versatility of the computer lies in its ability to perform a set of instructions repeatedly. </li></ul></ul><ul><ul><li>Loops are the set of instructions in a computer program that is repeated a particular number of times or until a specific objective has been achieved. </li></ul></ul>
3. 3. Loops Have: <ul><li>Initialization. </li></ul><ul><li>Condition to test to exit the loop. </li></ul><ul><li>Statements in the body of loop. </li></ul><ul><li>Increment or decrement that will change the value that determines whether you will stay in loop. </li></ul><ul><li>The order these may vary according to loop. </li></ul>
4. 4. Types of loop: <ul><ul><li>For loop. </li></ul></ul><ul><ul><li>While loop. </li></ul></ul><ul><ul><li>Do-While loop. </li></ul></ul>
5. 5. Syntax of loop(For): For (initialise counter;test counter;increment counter) { statement 1; other statements; }
6. 6. Recursion: <ul><ul><li>Recursion provides an alternate of loops to solve a problem. </li></ul></ul><ul><ul><li>Recursion is a function having a statement which call the same function. </li></ul></ul><ul><ul><li>Recursion is also called circular definition. </li></ul></ul>
7. 7. Stack: <ul><ul><li>A stack is a last-in/first out memory structure. The first item referenced or removed from a stack is always the last item entered into the stack. For example,a pile of books. </li></ul></ul><ul><ul><li>Memory for recursion calls is a Stack. </li></ul></ul>
8. 8. What's the structure of recursion <ul><ul><li>Base cases-One or more cases in which the function accomplished its task without the use of any recursive call. </li></ul></ul><ul><ul><li>Recursive cases-One or more caes in which function accomplishes its task by using recursive calls to accomplish one or more smaller versions of task. </li></ul></ul>
9. 9. Think before using recursion <ul><ul><li>What's the base case(s). </li></ul></ul><ul><ul><li>How to divide the original problem into sub problems. </li></ul></ul><ul><ul><li>How to merge the sub problem's results to get the final result. </li></ul></ul>
10. 10. General syntax: Function definition(variable(s)) { variable declaration; condition1 base case; condition 2 recursive case; return(variable); }
11. 11. Program of Factorial: <ul><ul><li>Factorial of a number is the the number resulting from multiplying a whole number by every whole number between itself and 1 inclusive. </li></ul></ul><ul><ul><li>6 factorial, or 6!, is 6 x 5 x 4 x 3 x 2 x 1 = 720. </li></ul></ul>
12. 12. Using For loop: Int factorial (int n) { int f=1,i; for(i=n;i>=1;i--) f=f*i; return (f); }
13. 13. Characteristics: <ul><ul><li>Initialization ,i=n(input number). </li></ul></ul><ul><ul><li>Loop operation,f=f*i – Executed n times. </li></ul></ul><ul><ul><li>Termination, i--,allows the condition(i>=1) to become false. </li></ul></ul>
14. 14. Using recursion: Int rec(int n) { int f; if(n==1) return (1); else f=n*rec(n-1); return (f); }
15. 15. Characteristics: <ul><ul><li>Base case: return (1). </li></ul></ul><ul><ul><li>Recursive case: return (f), f=n*rec(n-1). </li></ul></ul><ul><ul><li>Termination: It guaranteed by the function call rec(n-1).As soon as condition (n==1) is satisfied,code for base case is executed. </li></ul></ul>
16. 16. COMPARISION: <ul><ul><li>Both iteration and recursion are based on control structure.Iteration uses a repetition structure while recursion uses a selection structure. </li></ul></ul><ul><ul><li>Iteration explicitly uses a repetition structure,recursion achieves repetition through repeated calls. </li></ul></ul><ul><ul><li>Iteration terminates when loop condition fails,recursion terminates when base case is recognised. </li></ul></ul>
17. 17. Continue- <ul><ul><li>Iteration and recursion can occur infinitely: An infinite loop occurs with iteration if the loop-continuation test never becomes false; infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on the base case. </li></ul></ul><ul><ul><li>Recursion does use more space than iteration. </li></ul></ul>
18. 18. Recursion vs iteration <ul><ul><li>In the &quot;factorial&quot; example the iterative implementation is likely to be slightly faster in practice than the recursive one,because Recursion repeatedly invokes the mechanism, and consequently the overhead, of method calls. This can be expensive in both processor time and memory space. </li></ul></ul><ul><ul><li>There is always some limit to the size of stack and recursive algorithms tend to require more stack space than iterative algorithms. </li></ul></ul>