SlideShare a Scribd company logo
Iteration And recursion Which is better?
Loops: All languages need an iteration statement. The versatility of the computer lies in its ability to perform a set of instructions repeatedly. Loops are the set of instructions in a computer program that is repeated a particular number of times or until a specific objective has been achieved.
Loops Have: Initialization. Condition to test to exit the loop. Statements in the body of loop. Increment or decrement that will change the value that determines whether you will stay in loop. The order these may vary according to loop.
Types of loop: For loop. While loop. Do-While loop.
Syntax of loop(For): For (initialise counter;test counter;increment counter)  {  statement 1;  other statements;  }
Recursion: Recursion provides an alternate of loops to solve a problem. Recursion is a function having a statement which call the same function. Recursion is also called circular definition.
Stack: A stack is a last-in/first out memory structure. The first item referenced or removed from a stack is always the last item entered into the stack. For example,a pile of books. Memory for recursion calls is a Stack.
What's the structure of recursion Base cases-One or more cases in which the function accomplished its task without the use of any recursive call. Recursive cases-One or more caes in which function accomplishes its task by using recursive calls to accomplish one or more smaller versions of task.
Think before using recursion What's the base case(s). How to divide the original problem into sub problems. How to merge the sub problem's results to get the final result.
General syntax: Function definition(variable(s))  {  variable declaration;  condition1  base case;  condition 2  recursive case;  return(variable);  }
Program of Factorial: Factorial of a number is the the number resulting from multiplying a whole number by every whole number between itself and 1 inclusive.  6 factorial, or 6!, is 6 x 5 x 4 x 3 x 2 x 1 = 720.
Using For loop: Int factorial (int n)  {  int f=1,i;  for(i=n;i>=1;i--)  f=f*i;  return (f);  }
Characteristics: Initialization ,i=n(input number). Loop operation,f=f*i – Executed n times. Termination, i--,allows the condition(i>=1) to become false.
Using recursion: Int rec(int n)  {  int f;  if(n==1)  return (1);  else  f=n*rec(n-1);  return (f);  }
Characteristics: Base case: return (1). Recursive case: return (f), f=n*rec(n-1). Termination: It guaranteed by the function call rec(n-1).As soon as condition (n==1) is satisfied,code for base case is executed.
COMPARISION: Both iteration and recursion are based on control structure.Iteration uses a repetition structure while recursion uses a selection structure. Iteration explicitly uses a repetition structure,recursion achieves repetition through repeated calls. Iteration terminates when loop condition fails,recursion terminates when base case is recognised.
Continue- Iteration and recursion can occur infinitely: An infinite loop occurs with iteration if the loop-continuation test never becomes false; infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on the base case. Recursion does use more space than iteration.
Recursion vs iteration In the "factorial" example the iterative implementation is likely to be slightly faster in practice than the recursive one,because Recursion repeatedly invokes the mechanism, and consequently the overhead, of method calls. This can be expensive in both processor time and memory space. There is always some limit to the size of stack and recursive algorithms tend to require more stack space than iterative algorithms.

More Related Content

What's hot (20)

PPTX
Recursive Function
Kamal Acharya
 
PPTX
Turing machine and Halting Introduction
AmartyaYandrapu1
 
PPTX
Java interface
Md. Tanvir Hossain
 
PPTX
Turing Machine
arwa wshyar
 
PPTX
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
varun arora
 
PDF
Pumping lemma for cfl
Muhammad Zohaib Chaudhary
 
PPT
pushdown automata
Sujata Pardeshi
 
PPTX
Single source Shortest path algorithm with example
VINITACHAUHAN21
 
PPTX
Constructors in C++
RubaNagarajan
 
PPT
Lexical analysis, syntax analysis, semantic analysis. Ppt
ovidlivi91
 
PPT
C, C++ Interview Questions Part - 1
ReKruiTIn.com
 
PPTX
5 nested if in c with proper example
Alamgir Hossain
 
PPTX
String Matching (Naive,Rabin-Karp,KMP)
Aditya pratap Singh
 
PDF
Automata theory
Pardeep Vats
 
PDF
Cs8792 cns - unit iv
ArthyR3
 
PPTX
Looping statement in vb.net
ilakkiya
 
PPTX
Java interface
BHUVIJAYAVELU
 
PPTX
RSA algorithm
Arpana shree
 
PPTX
Java program structure
Mukund Kumar Bharti
 
PPTX
Diffie hellman key exchange algorithm
Sunita Kharayat
 
Recursive Function
Kamal Acharya
 
Turing machine and Halting Introduction
AmartyaYandrapu1
 
Java interface
Md. Tanvir Hossain
 
Turing Machine
arwa wshyar
 
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
varun arora
 
Pumping lemma for cfl
Muhammad Zohaib Chaudhary
 
pushdown automata
Sujata Pardeshi
 
Single source Shortest path algorithm with example
VINITACHAUHAN21
 
Constructors in C++
RubaNagarajan
 
Lexical analysis, syntax analysis, semantic analysis. Ppt
ovidlivi91
 
C, C++ Interview Questions Part - 1
ReKruiTIn.com
 
5 nested if in c with proper example
Alamgir Hossain
 
String Matching (Naive,Rabin-Karp,KMP)
Aditya pratap Singh
 
Automata theory
Pardeep Vats
 
Cs8792 cns - unit iv
ArthyR3
 
Looping statement in vb.net
ilakkiya
 
Java interface
BHUVIJAYAVELU
 
RSA algorithm
Arpana shree
 
Java program structure
Mukund Kumar Bharti
 
Diffie hellman key exchange algorithm
Sunita Kharayat
 

Similar to Recursion and looping (20)

PPTX
Recursion
Ashish Ranjan
 
DOCX
Recursion in C++
Maliha Mehr
 
PPTX
Unit-I Recursion.pptx
ajajkhan16
 
PPTX
Lecture_7_StackAndRecursion (1).pptx
AbuHuraira729502
 
PPTX
Recursive Algorithms with their types and implementation
Ahmad177077
 
PPT
Model and Design
Dr Shashikant Athawale
 
PPTX
Java script – basic auroskills (2)
BoneyGawande
 
PPTX
Python recursion
ToniyaP1
 
PPTX
Recursion vs. Iteration: Code Efficiency & Structure
cogaxor346
 
PPTX
Recursion
Jesmin Akhter
 
PPT
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
PDF
Chapter 7 recursion handouts with notes
mailund
 
PDF
Java Script Basics presentation of program
MarcosLuis32
 
PPTX
2nd year computer science chapter 12 notes
muhammadFaheem656405
 
PDF
Python recursion
Prof. Dr. K. Adisesha
 
PPT
C++ chapter 4
SHRIRANG PINJARKAR
 
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
PPT
recursive problem_solving
Rajendran
 
PPTX
2022-9-recursive-1.pptx
RayLee547290
 
PPTX
Introduction to Recursion
Mansoor Bahramand
 
Recursion
Ashish Ranjan
 
Recursion in C++
Maliha Mehr
 
Unit-I Recursion.pptx
ajajkhan16
 
Lecture_7_StackAndRecursion (1).pptx
AbuHuraira729502
 
Recursive Algorithms with their types and implementation
Ahmad177077
 
Model and Design
Dr Shashikant Athawale
 
Java script – basic auroskills (2)
BoneyGawande
 
Python recursion
ToniyaP1
 
Recursion vs. Iteration: Code Efficiency & Structure
cogaxor346
 
Recursion
Jesmin Akhter
 
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Chapter 7 recursion handouts with notes
mailund
 
Java Script Basics presentation of program
MarcosLuis32
 
2nd year computer science chapter 12 notes
muhammadFaheem656405
 
Python recursion
Prof. Dr. K. Adisesha
 
C++ chapter 4
SHRIRANG PINJARKAR
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
recursive problem_solving
Rajendran
 
2022-9-recursive-1.pptx
RayLee547290
 
Introduction to Recursion
Mansoor Bahramand
 
Ad

Recently uploaded (20)

PDF
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PPTX
𝙳𝚘𝚠𝚗𝚕𝚘𝚊𝚍—Wondershare Filmora Crack 14.0.7 + Key Download 2025
sebastian aliya
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PPTX
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
PDF
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
PDF
Database Benchmarking for Performance Masterclass: Session 1 - Benchmarking F...
ScyllaDB
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PDF
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
UiPath Agentic AI ile Akıllı Otomasyonun Yeni Çağı
UiPathCommunity
 
PDF
The Growing Value and Application of FME & GenAI
Safe Software
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PPTX
Simplifica la seguridad en la nube y la detección de amenazas con FortiCNAPP
Cristian Garcia G.
 
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
𝙳𝚘𝚠𝚗𝚕𝚘𝚊𝚍—Wondershare Filmora Crack 14.0.7 + Key Download 2025
sebastian aliya
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
Database Benchmarking for Performance Masterclass: Session 1 - Benchmarking F...
ScyllaDB
 
Kubernetes - Architecture & Components.pdf
geethak285
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
UiPath Agentic AI ile Akıllı Otomasyonun Yeni Çağı
UiPathCommunity
 
The Growing Value and Application of FME & GenAI
Safe Software
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Simplifica la seguridad en la nube y la detección de amenazas con FortiCNAPP
Cristian Garcia G.
 
Ad

Recursion and looping

  • 1. Iteration And recursion Which is better?
  • 2. Loops: All languages need an iteration statement. The versatility of the computer lies in its ability to perform a set of instructions repeatedly. Loops are the set of instructions in a computer program that is repeated a particular number of times or until a specific objective has been achieved.
  • 3. Loops Have: Initialization. Condition to test to exit the loop. Statements in the body of loop. Increment or decrement that will change the value that determines whether you will stay in loop. The order these may vary according to loop.
  • 4. Types of loop: For loop. While loop. Do-While loop.
  • 5. Syntax of loop(For): For (initialise counter;test counter;increment counter) { statement 1; other statements; }
  • 6. Recursion: Recursion provides an alternate of loops to solve a problem. Recursion is a function having a statement which call the same function. Recursion is also called circular definition.
  • 7. Stack: A stack is a last-in/first out memory structure. The first item referenced or removed from a stack is always the last item entered into the stack. For example,a pile of books. Memory for recursion calls is a Stack.
  • 8. What's the structure of recursion Base cases-One or more cases in which the function accomplished its task without the use of any recursive call. Recursive cases-One or more caes in which function accomplishes its task by using recursive calls to accomplish one or more smaller versions of task.
  • 9. Think before using recursion What's the base case(s). How to divide the original problem into sub problems. How to merge the sub problem's results to get the final result.
  • 10. General syntax: Function definition(variable(s)) { variable declaration; condition1 base case; condition 2 recursive case; return(variable); }
  • 11. Program of Factorial: Factorial of a number is the the number resulting from multiplying a whole number by every whole number between itself and 1 inclusive. 6 factorial, or 6!, is 6 x 5 x 4 x 3 x 2 x 1 = 720.
  • 12. Using For loop: Int factorial (int n) { int f=1,i; for(i=n;i>=1;i--) f=f*i; return (f); }
  • 13. Characteristics: Initialization ,i=n(input number). Loop operation,f=f*i – Executed n times. Termination, i--,allows the condition(i>=1) to become false.
  • 14. Using recursion: Int rec(int n) { int f; if(n==1) return (1); else f=n*rec(n-1); return (f); }
  • 15. Characteristics: Base case: return (1). Recursive case: return (f), f=n*rec(n-1). Termination: It guaranteed by the function call rec(n-1).As soon as condition (n==1) is satisfied,code for base case is executed.
  • 16. COMPARISION: Both iteration and recursion are based on control structure.Iteration uses a repetition structure while recursion uses a selection structure. Iteration explicitly uses a repetition structure,recursion achieves repetition through repeated calls. Iteration terminates when loop condition fails,recursion terminates when base case is recognised.
  • 17. Continue- Iteration and recursion can occur infinitely: An infinite loop occurs with iteration if the loop-continuation test never becomes false; infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on the base case. Recursion does use more space than iteration.
  • 18. Recursion vs iteration In the "factorial" example the iterative implementation is likely to be slightly faster in practice than the recursive one,because Recursion repeatedly invokes the mechanism, and consequently the overhead, of method calls. This can be expensive in both processor time and memory space. There is always some limit to the size of stack and recursive algorithms tend to require more stack space than iterative algorithms.