+ 5

What is the difference between iteration and recursion?

I have been confused about these two things since forever, could someone please give me an easy way to understand these two things

8th Jun 2019, 2:49 PM
Sudarshan Rai
Sudarshan Rai - avatar
10 odpowiedzi
+ 7
Iteration: repeatedly cycling through data using a loop, most commonly using a for loop or a while loop Ex: While(I<10){ I = I +1; } Recursion: invoking a method within itself with slightly altered arguments repeatedly until whats known as the "base case" is met. Ex: Method subtractToZero(int A){ If(a==0) Return 0; Else Return subtractToZero(a-1); If(a==0) is the base case, now in all honesty I really have no clue what thr method i just wrote does, the point is you call the method within itself altering the arguments, recursion is a mind boggling subject, something to look into would be the "stack" data structure because all functions are placed on the stack and solved from inside out, so in recursion the first call to the method will be the last thing that returns.
8th Jun 2019, 8:46 PM
StealthyTaco
StealthyTaco - avatar
+ 16
To understand iteration, you must: - wonder what a loop is - start looping to try it - continue looping - stop looping - think "well, i got it" To understand recursion, you must understand recursion
8th Jun 2019, 3:22 PM
Cépagrave
Cépagrave - avatar
+ 5
Iterative solutions are easier for beginners than recursive ones.
8th Jun 2019, 10:19 PM
Sonic
Sonic - avatar
+ 3
iteration is used for loops and recursion is used for functions. they kinda have similar meanings but recursion is when a function calls it self.
8th Jun 2019, 2:51 PM
Farry
Farry - avatar
+ 3
Iteration uses a list of inputs in a function operation to produce corresponding outcomes while recursion uses a result of an input as an input of the next same function operation.
10th Jun 2019, 11:50 AM
Dan Rhamba
Dan Rhamba - avatar
+ 2
Iteration=> it is used in loop and it is autoincremt as your need. Recursion=>calling a function by itself again and again.
9th Jun 2019, 4:42 AM
Ajay
Ajay - avatar
+ 2
Iteration : looping on an entity to traverse it's all values(by index/obj) Recursion : a function calls itself again and again to meet a criteria.
9th Jun 2019, 8:16 AM
Jai Verma
Jai Verma - avatar
+ 2
iteration is the concept of repeat execution of same code it is only part of program recursive is the process of calling function itself ditectly or indirectly this concept plays a viotal role in design of algorithms because the recursive functions make complicated code simpler and improve high reuability and reliability
9th Jun 2019, 8:21 PM
sree harsha
sree harsha - avatar
+ 1
With recursion, namespaces may cause problems, which should not appear with loops.
8th Jun 2019, 3:28 PM
Seb TheS
Seb TheS - avatar
+ 1
Iteration is concern with loops while recursion with functions. Every cycle of loop is iteration in which the loop code execute. In recursion function body executed for every recursive call by the same function.
8th Jun 2019, 5:50 PM
Shubham Tandale
Shubham Tandale - avatar