can someone please clarify the difference between iterative and recursive?
Simply : The recursive function calls itself (for different parameters) and inside it you should define a Stopping codition : This condition to tell the function to stop calling itself ex : The factorial numer recursively : int fact (int n) { if (n==0 || n==1) return n; else return n * fact(n-1); } ...................... The iterative function uses a loop (while , for , do-while ..) this loop reapeats itself until some condition is true ex : The factorial number Iteratively : int fact (int n) { int result =1; for (int i = 1 ; i <=n ; i++) result *= i; return result; } that means : recursive : calling the same function to itself more than one time . iterative : in The function calling the same block more than one time . There is many advantages and disadvantages of each type . some of them : recursive is harder to understand for biggeners recursive will be slower for greater resourses (in our example : for big numbers) recursive will be the easiest way for the experts (it uses a very logic way) in recursive The program may be shorter (hanoi towers in recursive can be solved in some statements but it will be hard to solve it in iterative)
in iterative we iterate statements a no.of times by using looping statements while in recursives we iterate the whole function,
iterative means going through a specific kind of loop, recursive function you keep going within the function (usually to get the root of the function)
also in recursive stack operations are used.that is when you call a recursive procedure it gets pushed into the stack until the condition for termination gets satisfied and from then it gets popped. but in iterative u only loop it but you dont have nything like the stack operations and son on... so it depends on your application whether recursive or itersative is the better one.
Think of recursive functions like the movie "Inception". The function/dream keeps calling itself until it has accomplished it's goals, then it quits out of the other functions/dreams until it makes its way out. Iterative is just a normal loop. Everything that can be done recursively can also be done iteratively.
Join our real-time social learning platform and learn together with your friends!