Title | Differences between Recursion and Iteration |
---|---|
Author | Maggie Kiai |
Course | Information Technology |
Institution | Technical University of Kenya |
Pages | 2 |
File Size | 77.2 KB |
File Type | |
Total Downloads | 21 |
Total Views | 158 |
Differences between recursion and iteration. This is an essay giving you the detailed differences between recursion and iteration....
Differences between Recursion and Iteration A program is called recursive when an entity calls itself. A program is call iterative when there is a loop Recursion Recursion is usually slower than iteration due to the overhead of maintaining the stack. Recursion uses more memory than iteration. Recursion makes the code smaller. Recursion uses selection structure. Infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on some condition (base case) and Infinite recursion can crash the system. Recursion terminates when a base case is recognized.
Java program to find factorial of given number class GFG { // ----- Recursion ----// method to find factorial of given number static int factorialUsingRecursion(int n) { if (n == 0) return 1; // recursion call return n * factorialUsingRecursion(n - 1); }
Iteration
Iteration uses repetition structure. An iteration does not use the stack so it's faster than recursion. An infinite loop occurs with iteration if the loop condition test never becomes false and Infinite looping uses CPU cycles repeatedly.
Iteration makes the code longer. An iteration terminates when the loop condition fails.
Iteration consumes less memory.
ITERATION Java program to find factorial of given number class GFG Iteration static int factorialUsingIteration(int n) { int res = 1, i; // using iteration for (i = 2; i...