General Discussions

Iteration and Recursion

Iteration
is the act of repeating a process with the aim of approaching a desired result. The result of one iteration, mostly, goes as input to next iteration. It is repetition of block of statements in a computer program to carry out a task.

Recursion
can be defined by two properties:

  1. It has a simple base case (or cases)
  2. Then it has a set of rules that reduce all other cases towards the base case

Comparing and contrasting both of them:

Iteration repeats until condition fails. Recursion calls itself until base condition is reached.

Iteration involves: initialization, condition, execution and updation. In recursive function, only terminate condition is specified.

Iteration makes code longer and recursion keeps it short.

Iteration is faster and recursion is slower due to overhead of stack maintenance.

For above mentioned reason recursion takes more memory that iteration.

Infinite recursion can crash the system. Infinite loop uses CPU cycles repeatedly.

Note:
Iterative approach is more efficient in terms of memory utilization and speed of execution. Don’t go for recursion for simple programs or programs which are not recursive in nature. 

Advertisements

2 thoughts on “Iteration and Recursion

Let me Know What you Think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s