+ 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
10 Respuestas
+ 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.
+ 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
+ 5
Iterative solutions are easier for beginners than recursive ones.
+ 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.
+ 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.
+ 2
Iteration=> it is used in loop and it is autoincremt as your need.
Recursion=>calling a function by itself again and again.
+ 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.
+ 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
+ 1
With recursion, namespaces may cause problems, which should not appear with loops.
+ 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.