answersLogoWhite

0


Best Answer

Both Iteration and Recursion run a part of an algorithm repeatedly. We typically use them when, in a part of an algorithm, we have to do or calculate something (i,e. the logic remains the same though the data changes) for a certain number of times. However,in Recursion, we simply run a loop for a certain number of times in the algorithm, such as printing a statement ten times is recursion. In Iteration, we specifically run a loop, such that, the second run of the loop makes use of the computation/result from the first run, the third run (or iteration) makes use of the result from the second run and so on. Hence, in Iteration, the n th run of the loop makes use of the result from the n-1 th run. Consider the following example for calculating the summation, which we will denote as sum(n), meaning n + n-1 + n-2 + n-3 + ... + 2 + 1 + 0. Hence, sum(5) = 5+4+3+2+1+0 = 15. Now we will write psuedo-code to calculate the sum(n) using Iteration and Recursion. Iterative Version:- ----------------- start sum(n) { i = 0; total = 0; do while i is not greater than n, { total = total + i; i = i + 1; } return total; } Recursive Version:- ----------------- start sum(n) { if n is equal to 0, then return(0); otherwise, return (n + sum(n - 1)); } Notice that the Iterative version calculates the sum by repeatedly accumulating the sum in the variable total. In contrast, the recursive version simply repeatedly calls itself, each time with a decremented value.

.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

A recursive function works through the process of calling itself until a condition is met.

e.g. While creating Fibonacci series or

calculating multiple of a number

We simply call the function repeatedly.

Iteration uses a looping control structure (while, do while, for) to repeat a section of code until a condition is met.

Frankly, Anything done in one style of looping, can be done in the other.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Some problems are easier to solve with recursion, even if the easier solution is often the less effective. Example: Calculating the Fibonacci numbers.

Recursive:

int Fib (int n)

{

if (n<=2) return 1;

else return Fib (n-1)+Fib (n-2);

}

Iterative

int Fib (int n)

{

int f1=1, f2=1, j;

for (j=2; j<n; ++j) {

tmp = f1;

f1= f2;

f2= f2+tmp;

}

return f2;

}

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Recursion and iteration are two different ways to solve a problem by executing the same code repeatedly. Generally, the code will repeatedly perform the exact same algorithm upon some data, changing that data in some way until some exit condition is satisfied. Recursion is typically used in divide-and-conquer algorithms where a large problem is repeatedly divided into one or more smaller instances of the same problem until each instance is small enough to be resolved. The algorithm then backtracks through the instances, re-combining the smaller solutions into the larger solution until the whole problem is solved. Iteration is typically used for trivial loops where backtracking is not a requirement.

1. Recursion is implemented by calling a function that calls itself whereas iteration uses a structured loop statement (such as for, while or do-while).

2. With iteration there is only one set of local variables involved whereas recursion instantiates a new set of local variables each time the function is invoked.

3. Recursion allows automatic backtracking whereas iteration does not.

4. Recursive algorithms can be inline expanded whereas iterative algorithms cannot.

5. Recursive algorithms can be computed at compile-time whereas iterative algorithms cannot.

6. Although iteration can occasionally be more efficient than recursion, the complexity of the code can hinder readability and increase maintenance costs.

7. If an algorithm is not naturally recursive, iteration is the only option.

8. If an exit condition can never be satisfied, a recursive loop will eventually consume the stack and crash the program, whereas an iterative loop may execute indefinitely.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Mostimpotentdifference between recursion and looping is that,

recursion is based upon 2 basic rules.


1. There must be a base statement on which recursion is to be dependent.

2. At each call ofrecursion, the calling statement must be coming near the base statement.


Looping may or may notfollowtheserules but recursion must follow these rules.


ex

get(int i)

{

if(i = 0)//Base Criteria

return;

else {

cout<

i--;

get(i);//Recursive Call

}
}


void main()

{

int num =5;

get(num);//54321 by recursion

while(num>0)//By looping

{

cout<

num--;//It is Optional, but recomended if we r not requiring infinite looping

}


}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Synonyms.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between looping and recursion?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Bring out the difference between recursion and iteration?

Recursion is repeatedly calling the function....---- whereas Iteration is nothing but just looping until condition doesn't satisfy.....


What is difference between control statements and looping statements in c language?

differance between control statement and looping statement?


What is the difference between left recursion and right recursion in a grammar?

Recursion is what it's called when a function calls itself. When a function calls itself immediately before returning, it's called tail recursion. Tail recursion can be more efficiently written as iteration. In fact a good compiler will recognize tail recursion and compile it as iteration. There is no such thing as left or right recursion in C programming.


What is difference between branching and looping?

looping run condition contiously till a specific condition is true ,otherwise branching test condition at a time.


What is the difference between conditional statements and looping statements?

The main difference is how often they run. A conditional statement - often an IF - may or may not run. If it does (depending on whether a condition is fulfilled), it runs only once. A looping statement - often a WHILE or FOR - is designed to repeat, while a certain condition is true.


What is looping statement?

What is looping statement?


What is the use of recursion function?

Read the part in your programming manual/text book about recursion. The short answer while easy does not tell you anything about the power or dangers of recursion. It is the power and dangers of recursion that is important because once understood you can use recursion to good effect without running out of stack space.


Why do i get Pops in pro tools when looping tracks from reason?

sample rate difference. they have to be set to the same sample rate


How many types of recursion are there in c language?

Recursion in c language is a method where the function calls itself, within or outside the scope. Using Recursion, complicated problems can be divided into smaller parts so that solving them becomes more manageable. The recursion technique is available in Java, JavaScript, and C++.serves the same purpose. The type of Recursion in C • Direct Recursion • Indirect Recursion. Direct Recursion Recursion can call the function n-number of times. In the case of direct Recursion, the function calls itself inside the same position or in the local scope Direct Recursion problems are the Fibonacci series, a program to print 50 natural numbers. Indirect Recursion In the case of Indirect Recursion, a function X calls function Y, and function Y calls any function Z. Under certain conditions, function Z calls function A. In this case, function A is indirectly related to function Z. Indirect Recursion is also known as mutual Recursion, as more than one function runs a program. It is a two-step recursive function call process for making a recursive function call. Below mentioned are also type of Recursion: Tail Recursion No Tail/Head Recursion Linear Recursion Tree Recursion Tail Recursion A function is said to be tail recursion if it calls itself and also calls the last or the previous statement executed in the process. Head Recursion A function is said to be Head Recursion if it calls itself and also calls the first or the beginning statement executed in the process. Linear Recursion A function is said to be a linear recursive function if it makes a single call to itself each time the procedure executes itself and grows linearly depending on the size of the problem. Tree Recursion Tree Recursion is different from linear Recursion. Rather than making only one call to itself, that function makes more than one recursive call to the process within the recursive function. Following are the steps to solve the recursive problem in C: Step 1: Create a function and assign the work a part should do. Step 2: Select the subproblem and assume that the function already works on the problem. Step 3: Get the answer to the subproblem and use it to resolve the main issue. Step 4: The 90% of the problem defined is solved.


What is direct recursion?

When a function calls itself it is called as direct recursion. A function calls other functions which eventually call the original function is called as indirect recursion.


What is Looping in writing?

Looping is linking your thoughts to expand on your writing.


What type of science is recursion?

Recursion is a computer science. Typically computer programers write a specific program to test out a theory of recursion based on the known facts to try to define the variable.