0% found this document useful (0 votes)
63 views

Iteration and Recursion

This document discusses iteration and recursion in computer science. It provides examples of using iteration and recursion to calculate the factorial of a number. While iteration uses loops, recursion uses function calls that call the same function. Both iteration and recursion can be used interchangeably to repeatedly execute a set of instructions, with the main difference being recursion uses function calls while iteration uses loops. The document concludes that recursion and iteration can be interchangeably to solve problems like calculating factorials, with the choice depending on code size and time complexity needs.

Uploaded by

Debmalya Dash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Iteration and Recursion

This document discusses iteration and recursion in computer science. It provides examples of using iteration and recursion to calculate the factorial of a number. While iteration uses loops, recursion uses function calls that call the same function. Both iteration and recursion can be used interchangeably to repeatedly execute a set of instructions, with the main difference being recursion uses function calls while iteration uses loops. The document concludes that recursion and iteration can be interchangeably to solve problems like calculating factorials, with the choice depending on code size and time complexity needs.

Uploaded by

Debmalya Dash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT

CC – 148/294
UNDER MAKAUT, WB

Iteration and recursion can be used


interchangeably
- Justify

CONTINUOUS ASSESSMENT 1
Data Structure & Algorithm (ES)
PCC-CS301

Presented by
DEBMALYA DASH
14800321063
ECE 3rd SEM

AY : 2022-23
Table of Content

01 02 03 04 05 06
What is What is Recursion vs Example of Can Iteration and Reference
Iteration? Recursion? Iteration Iteration and recursion be used
Recursion interchangeably ?
What is Iteration?

The iteration can be defined as repeating the same process multiple times until a specific
condition satisfies. It is also known as looping.

There are mainly two types of loops :


Entry Controlled loops : In this type of loop, the test condition is tested before entering the
loop body. For Loop and While Loop is entry-controlled loops.

Exit Controlled Loops : In this type of loop the test condition is tested at the end of the loop
body. Therefore, the loop body will execute at least once, irrespective of whether the test
condition is true or false. the do-while loop is exit controlled loop.
for (initialization expr; test expr; update expr)
{
Syntax of For loop : // body of the loop
// statements we want to execute
}

initialization expression;
while (test_expression)
{
Syntax of While loop : // statements

update_expression;
}

initialization expression;
do
{
// statements
Syntax of do-while loop :
update_expression;
} while (test_expression);
What is Recursion?
Recursion is the process of repeating items in a self-similar way. In programming languages, if a
program allows you to call a function inside the same function, then it is called a recursive call of
the function.
Working of Recursion :
void recurse()
{ recursive
... .. ... call
recurse();
... .. ...
}
int main()
{
... .. ...
recurse();
... .. ...
}

The recursion continues until some condition is met to prevent it.


To prevent infinite recursion, if...else statement (or similar approach) can be used where one
branch makes the recursive call, and other doesn't.
Recursion vs Property Recursion Iteration

Iteration Definition Function calls itself.


A set of instructions repeatedly
executed.

Application For functions. For loops.

When the termination


Through base case, where
Termination condition for the iterator
there will be no function call.
ceases to be satisfied.

Used when code size needs to Used when time complexity


Usage be small, and time needs to be balanced against
complexity is not an issue. an expanded code size.

Code Size Smaller code size Larger Code Size.

Very high(generally Relatively lower time


Time Complexity exponential) time complexity(generally
complexity. polynomial-logarithmic).
Example of Iteration
For example let’s take an example to calculate factorial of a Start

given number.
Declare i,
Program : Flowchart : fact=1, num

#include<stdio.h> Read num


int main()
{
int i,fact=1,num; i=1
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++){
fact=fact*i; Is False
} i<=num Print fact
printf("Factorial of %d is: %d",num,fact);
return 0;
} True

i++ fact = fact*i ; Stop


Example of Recursion
Now let’s calculate the factorial of a given number using
recursion.
#include<stdio.h>
Program : int fact(int n)
{
if (n == 0)
return 1;
else
return(n * fact(n-1));
}
void main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %ld\n", num, fact(num);
return 0;
}
Can Iteration and recursion be used
interchangeably ?

Yes, absolutely, iteration and recursion can be used interchangeably.

As we can see from the previous two example the same problem is done using both Iteration and
Recursion. Not only this problem any problems can be using both of this method.

The concept of Recursion and Iteration is to execute a set of instructions repeatedly. The
difference between them is that recursion is simply a method call in which the method being
called is the same as the one making the call while iteration is when a loop is repeatedly executed
until a certain condition is met. Under the hood, both recursion and iteration depends on a
condition so as to know when to stop but recursion is simply a process, always applied to a
function.
Reference

To do this presentation I took help from

www.geeksforgeeks.org
https://stackoverflow.com
Let Us C Book by Yashavant Kanetkar

You might also like