recursion and looping
DESCRIPTION
A short comparison of iteration and recursion using example of factorial program.TRANSCRIPT
![Page 1: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/1.jpg)
Iteration And recursion
Which is better?
![Page 2: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/2.jpg)
Loops:
All languages need an iteration statement. The versatility of the computer lies in its
ability to perform a set of instructions repeatedly.
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.
![Page 3: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/3.jpg)
Loops Have:
Initialization. Condition to test to exit the loop. Statements in the body of loop. Increment or decrement that will change the
value that determines whether you will stay in loop.
The order these may vary according to loop.
![Page 4: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/4.jpg)
Types of loop:
For loop. While loop. Do-While loop.
![Page 5: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/5.jpg)
Syntax of loop(For):
For (initialise counter;test counter;increment counter)
{ statement 1; other statements;
}
![Page 6: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/6.jpg)
Recursion:
Recursion provides an alternate of loops to solve a problem.
Recursion is a function having a statement which call the same function.
Recursion is also called circular definition.
![Page 7: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/7.jpg)
Stack:
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.
Memory for recursion calls is a Stack.
![Page 8: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/8.jpg)
What's the structure of recursion
Base cases-One or more cases in which the function accomplished its task without the use of any recursive call.
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.
![Page 9: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/9.jpg)
Think before using recursion
What's the base case(s). How to divide the original problem into
sub problems. How to merge the sub problem's results to
get the final result.
![Page 10: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/10.jpg)
General syntax:
Function definition(variable(s)) {
variable declaration; condition1 base case; condition 2 recursive case; return(variable);
}
![Page 11: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/11.jpg)
Program of Factorial:
Factorial of a number is the the number resulting from multiplying a whole number by every whole number between itself and 1 inclusive.
6 factorial, or 6!, is 6 x 5 x 4 x 3 x 2 x 1 = 720.
![Page 12: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/12.jpg)
Using For loop:
Int factorial (int n) {
int f=1,i; for(i=n;i>=1;i--) f=f*i; return (f);
}
![Page 13: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/13.jpg)
Characteristics:
Initialization ,i=n(input number). Loop operation,f=f*i – Executed n times. Termination, i--,allows the condition(i>=1)
to become false.
![Page 14: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/14.jpg)
Using recursion:
Int rec(int n) {
int f; if(n==1)
return (1); else
f=n*rec(n-1); return (f);
}
![Page 15: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/15.jpg)
Characteristics:
Base case: return (1). Recursive case: return (f), f=n*rec(n-1). Termination: It guaranteed by the function
call rec(n-1).As soon as condition (n==1) is satisfied,code for base case is executed.
![Page 16: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/16.jpg)
COMPARISION:
Both iteration and recursion are based on control structure.Iteration uses a repetition structure while recursion uses a selection structure.
Iteration explicitly uses a repetition structure,recursion achieves repetition through repeated calls.
Iteration terminates when loop condition fails,recursion terminates when base case is recognised.
![Page 17: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/17.jpg)
Continue-
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.Recursion does use more space than iteration.
![Page 18: Recursion and looping](https://reader036.vdocuments.us/reader036/viewer/2022082921/55586868d8b42aaa7e8b49ae/html5/thumbnails/18.jpg)
Recursion vs iteration
In the "factorial" 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.
There is always some limit to the size of stack and recursive algorithms tend to require more stack space than iterative algorithms.