SlideShare a Scribd company logo
REVISION
Martin Chapman
martin.chapman@kcl.ac.uk
•By demand, from highest to lowest:
•Recursion
•Break
•Recursion
•Objects and Classes
•Break
•Arrays and Array Lists
RECURSION
Martin Chapman
martin.chapman@kcl.ac.uk
What do we need to make a loop run?
for (int i = 0; i<=10; i++) {
	 	 	
	 System.out.println(i);
	 	 	
}
for (int i = 0; i<=10; i++) {
	 	 	
	 System.out.println(i);
	 	 	
}
What do we need to make a loop run?
the ‘movement’
for (int i = 0; i<=10; i++) {
	 	 	
	 System.out.println(i);
	 	 	
}
What do we need to make a loop run?
the ‘change’
for (int i = 0; i<=10; i++) {
	 	 	
	 System.out.println(i);
	 	 	
}
What do we need to make a loop run?
‘when to stop’
for (int i = 0; i<=10; i++) {
	 	 	
	 System.out.println(i);
	 	 	
}
What do we need to make a loop run?
the content
How do we loop without a loop?
How do we loop without a loop?
the ‘movement’
the ‘change’
How do we loop without a loop?
‘when to stop’
How do we loop without a loop?
the content
How do we loop without a loop?
How do we loop without a loop?
base case
recursive case
Reverse, reverse.
What happens here?
Reverse, reverse.
methodA
3
3
Reverse, reverse.
methodA
3
3
3
3
3
Reverse, reverse.
methodA
3
3
3
Reverse, reverse.
methodA
3
2
2
methodA
3
Reverse, reverse.
methodA
3
2
!
!
2
3
methodA
If we tell the compiler to execute non-
conditional code then at some point
it must be executed.
Reverse, reverse.
methodA ! 3
Reverse, reverse.
methodA
3
2
!
2
methodA
Reverse, reverse.
methodA
3
2
!
2
methodA
2
2
2
Reverse, reverse.
methodA
3
2
!
methodA
2
2
Reverse, reverse.
methodA
3
2
!
methodA
1
1
methodA
2
Reverse, reverse.
methodA
3
2
!
methodA
1
1
!
!
methodA
2
Reverse, reverse.
methodA !
methodA !
3
2
Reverse, reverse.
methodA
1
!
methodA
0
!
methodA !
-1
methodA
-1
Reverse, reverse.
methodA
1
!
methodA
0
!
methodA !
-1
methodA
-1
-1
Reverse, reverse.
methodA
1
!
methodA
0
!
methodA !
-1
methodA
End of the method, so
return
Reverse, reverse.
methodA
1
!
methodA
0
!
methodA !
-1
methodAbase case
Reverse, reverse.
methodA !
methodA !
methodA !
methodA ! 0
1
2
3
Reverse, reverse.
methodA !
methodA !
methodA !
methodA ! 0
1
2
3
Reverse, reverse.
methodA !
methodA !
methodA !
methodA ! 0
1
2
3
Reverse engineering is fine, but
how do I build something from scratch?
0 1 2 3 4 5 6 7 8 9 10
0 1 1 2 3 5 8 13 21 34 55
0 1 2 3 4 5
0 1 1 2 3 5
Fibonacci Sequence
Every number is the sum of the
two previous, when possible.nth
fib
Programming in Java: Recursion
base case
Ask, when do we not need to do any
computation? When can we simply return a number
rather than doing any addition?
Determining the
0 1 2 3 4 5
0 1 1 2 3 5
Ask, when do we not need to do any
computation? When can we simply return a number
rather than doing any addition?
Determining the
0 1 2 3 4 5
0 1 1 2 3 5
base case
Programming in Java: Recursion
Programming in Java: Recursion
0
0
0 1 2 3 4 5
base case
Ask, when do we not need to do any
computation? When can we simply return a number
rather than doing any addition?
Determining the
0 1 1 2 3 5
1
1
0 1 1 2 3 5
Determining the
recursive case
0 1 2 3 4 5
What is the general case? Can this case be carried on
indefinitely?
0 1 1 2 3 5
0 1 2 3 4 5
Determining the
+ =
recursive case
What is the general case? Can this case be carried on
indefinitely?
0 1 1 2 3 5
0 1 2 3 4 5
Determining the
+ =
recursive case
+ =
What is the general case? Can this case be carried on
indefinitely?
+
1
1
0
0
fib(0) fib(1)
fib(2) =
static int fib(int n) {
	 	
	 	 if (n == 0) { return 0; }
	 	
	 	 if (n == 1) { return 1; }
	 	
	 	 else {
	 	 	 return fib(n-1) + fib(n-2); }
	 	
	 }
2
1 0
Determining the
recursive case
0 1 1 2 3 5
0 1 2 3 4 5
+ =
+ =
fib
( )
fib
( )
fib
( )
0 1 2 3 4 5
fib
( )
fib
( )
fib
( )
Determining the
recursive case
0 1 1 2 3 5+ =
+ =
0 1 2 3 4 5
fib
( )
fib
( )
fib
( )
Determining the
recursive case
0 1 1 2 3 5+ =
+ =
0 1 5
fib
( n )
fib
(n-2)
fib
(n-1)
Determining the
recursive case
0 1 1 2 3 5+ =
+ =
This operation is consistent, so we can generalise.
0 1 5
fib
( n )
fib
(n-2)
fib
(n-1)
Determining the
recursive case
0 1 1 2 3 5+ =
+ =
static int fib(int n) {
	 	
	 	 if (n == 0) { return 0; }
	 	
	 	 if (n == 1) { return 1; }
	 	
	 	 else {
	 	 	 return fib(n-1) + fib(n-2); }
	 	
	 }
static int fib(int n) {
	 	
	 	 if (n == 0) { return 0; }
	 	
	 	 if (n == 1) { return 1; }
	 	
	 	 else {
	 	 	 return fib(n-1) + fib(n-2); }
	 	
	 }
5
Recursion creates a tree
1 = actual value
1 0
1 1 0 01
1
1 = actual value
1 0
1 1 0 01
+
1
1 = actual value
1 0
1 1 0 01
+
+
1
1 = actual value
1 0
1 1 0 01
+
+
+
!
!
1
static int fib(int n) {
	 	
	 	 if (n == 0) { return 0; }
	 	
	 	 if (n == 1) { return 1; }
	 	
	 	 else {
	 	 	 return fib(n-1) + fib(n-2); }
	 	
	 }
!
1 = actual value
1 0
1 1 0 01
+
+
+
!
!
1
1 = actual value
1 0
1 1 0 01
+
+
+
!
!!
1
+
1 = actual value
1 0
1 1 0 01
1
+
+
+
!
!!
1
+
0 1 2 3 4 5
0 1 1 2 3 5
Fibonacci Sequence
Every number is the sum of the
two previous, when possible.
1 = actual value
1 0
1 1 0 01
1
+
+
+
!
!!
1
+
1 = actual value
1 0
1 1 0 01
1
+
+
+
!
!!
1
+
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
!
!!
1
+
0 1 2 3 4 5
0 1 1 2 3 5
Fibonacci Sequence
Every number is the sum of the
two previous, when possible.
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
!
!!
1
+
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
!
!!
+
1
+
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
!
!
1
+
1
+
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
!
!
1
+
1
+
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
!
!
1
3
+
1
+
+
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
!
!
1
3
+
+
1
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
!
!
1
3
+
++
1
+
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
!
1
3
+
++
1
1
+
+
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
!
1
3
+
++
1
1
+
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
1
3
+
++
1
1
2
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
1
3
+
++
1
1
2+
1 = actual value
1 0
1 1 0 01
1
+
+
+
2
1
3
+
++
1
1
2
5
+
static int fib(int n) {
	 	
	 	 if (n == 0) { return 0; }
	 	
	 	 if (n == 1) { return 1; }
	 	
	 	 else {
	 	 	 return fib(n-1) + fib(n-2); }
	 	
	 }
Think, when do I not have to do any computation?
This will also be when my code stops.
base case
SUMMARY
base case
static int fib(int n) {
	 	
	 	 if (n == 0) { return 0; }
	 	
	 	 if (n == 1) { return 1; }
	 	
	 	 else {
	 	 	 return fib(n-1) + fib(n-2); }
	 	
	 }
When I do have to compute something, what is the
pattern? If I can prove it for one case, it should
work for them all.
recursive case
SUMMARY
base case
base case
During the break...
SUMMARY
• Recursion is just another way of looping, with information
changing each time.
• Recursion consists of a recursive call and a base case.
• If there is code after a recursive call it must be executed
and will be executed in reverse order.
• Recursion is a natural solution if you can visualise the call in
any instance i.e. Fibonacci numbers.
WILL I SEE RECURSION
AGAIN?
• Yes, sorry.
• But it will give you more of a chance to understand it.
• Modules which touch on recursion (at least): DST (Semester
2, 1st year) and Algorithm Modules.

More Related Content

What's hot (20)

PPTX
Functions in c language
tanmaymodi4
 
PDF
Asymptotic Notation
sohelranasweet
 
PDF
Python for data science
Wei-Wen Hsu
 
PDF
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
PPT
Data Structures - Searching & sorting
Kaushal Shah
 
PDF
Time to-live: How to Perform Automatic State Cleanup in Apache Flink - Andrey...
Flink Forward
 
PPTX
DFS and BFS
satya parsana
 
PDF
Application of hashing in better alg design tanmay
Tanmay 'Unsinkable'
 
PDF
pandas - Python Data Analysis
Andrew Henshaw
 
PPTX
Stack Data Structure
Afaq Mansoor Khan
 
PPTX
Stack using Linked List
Sayantan Sur
 
PPTX
Destructors
DeepikaT13
 
PDF
Web Technology End semester Examination Questions
Ansuman Mahapatra
 
PPTX
Searching & Sorting Algorithms
Rahul Jamwal
 
PPT
17. Trees and Graphs
Intro C# Book
 
PPT
Asymptotic notations
Ehtisham Ali
 
PPTX
Advanced Sorting Algorithms
Damian T. Gordon
 
PDF
A Deep Dive into Stateful Stream Processing in Structured Streaming with Tath...
Databricks
 
PPTX
Chapter 14 strings
Praveen M Jigajinni
 
PDF
Algorithm chapter 10
chidabdu
 
Functions in c language
tanmaymodi4
 
Asymptotic Notation
sohelranasweet
 
Python for data science
Wei-Wen Hsu
 
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
Data Structures - Searching & sorting
Kaushal Shah
 
Time to-live: How to Perform Automatic State Cleanup in Apache Flink - Andrey...
Flink Forward
 
DFS and BFS
satya parsana
 
Application of hashing in better alg design tanmay
Tanmay 'Unsinkable'
 
pandas - Python Data Analysis
Andrew Henshaw
 
Stack Data Structure
Afaq Mansoor Khan
 
Stack using Linked List
Sayantan Sur
 
Destructors
DeepikaT13
 
Web Technology End semester Examination Questions
Ansuman Mahapatra
 
Searching & Sorting Algorithms
Rahul Jamwal
 
17. Trees and Graphs
Intro C# Book
 
Asymptotic notations
Ehtisham Ali
 
Advanced Sorting Algorithms
Damian T. Gordon
 
A Deep Dive into Stateful Stream Processing in Structured Streaming with Tath...
Databricks
 
Chapter 14 strings
Praveen M Jigajinni
 
Algorithm chapter 10
chidabdu
 

Similar to Programming in Java: Recursion (20)

PDF
Recursion.pdf
Flavia Tembo Kambale
 
PPTX
Recursion
SAGARDAVE29
 
PPTX
Recursion
Jesmin Akhter
 
PPT
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
PPT
FUNDAMETAL ALG.ppt
Menaka Sivakumar
 
PPT
Lec-6 Recursion of Data Structures & Algorithms
haseebanjum2611
 
PPT
14 recursion
Himadri Sen Gupta
 
PPT
Savitch ch 14
Terry Yoast
 
PPT
Cis068 08
FALLEE31188
 
PPT
Savitch Ch 14
Terry Yoast
 
PDF
Preparation Data Structures 02 recursion
Andres Mendez-Vazquez
 
PDF
Recursion For the Rest of Us (CS Fundamentals Series)
Haseeb Qureshi
 
PPTX
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
PPTX
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
usha raj
 
PPTX
Introduction to Dynamic Programming.pptx
PochupouOwo
 
PDF
Stanford splash spring 2016 basic programming
Yu-Sheng (Yosen) Chen
 
PDF
Data Structure - Lecture 2 - Recursion Stack Queue.pdf
donotreply20
 
PDF
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
PDF
062636636366363773737373733+73737733+7.pdf
GauravKumar295392
 
Recursion.pdf
Flavia Tembo Kambale
 
Recursion
SAGARDAVE29
 
Recursion
Jesmin Akhter
 
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
FUNDAMETAL ALG.ppt
Menaka Sivakumar
 
Lec-6 Recursion of Data Structures & Algorithms
haseebanjum2611
 
14 recursion
Himadri Sen Gupta
 
Savitch ch 14
Terry Yoast
 
Cis068 08
FALLEE31188
 
Savitch Ch 14
Terry Yoast
 
Preparation Data Structures 02 recursion
Andres Mendez-Vazquez
 
Recursion For the Rest of Us (CS Fundamentals Series)
Haseeb Qureshi
 
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
usha raj
 
Introduction to Dynamic Programming.pptx
PochupouOwo
 
Stanford splash spring 2016 basic programming
Yu-Sheng (Yosen) Chen
 
Data Structure - Lecture 2 - Recursion Stack Queue.pdf
donotreply20
 
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
062636636366363773737373733+73737733+7.pdf
GauravKumar295392
 
Ad

More from Martin Chapman (20)

PDF
Phenoflow: An Architecture for FAIRer Phenotypes
Martin Chapman
 
PDF
Generating Computable Phenotype Intersection Metadata Using the Phenoflow Lib...
Martin Chapman
 
PDF
Principles of Health Informatics: Artificial intelligence and machine learning
Martin Chapman
 
PDF
Principles of Health Informatics: Clinical decision support systems
Martin Chapman
 
PDF
Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...
Martin Chapman
 
PDF
Technical Validation through Automated Testing
Martin Chapman
 
PDF
Scalable architectures for phenotype libraries
Martin Chapman
 
PDF
Using AI to understand how preventative interventions can improve the health ...
Martin Chapman
 
PDF
Using AI to autonomously identify diseases within groups of patients
Martin Chapman
 
PDF
Using AI to understand how preventative interventions can improve the health ...
Martin Chapman
 
PDF
Principles of Health Informatics: Evaluating medical software
Martin Chapman
 
PDF
Principles of Health Informatics: Usability of medical software
Martin Chapman
 
PDF
Principles of Health Informatics: Social networks, telehealth, and mobile health
Martin Chapman
 
PDF
Principles of Health Informatics: Communication systems in healthcare
Martin Chapman
 
PDF
Principles of Health Informatics: Terminologies and classification systems
Martin Chapman
 
PDF
Principles of Health Informatics: Representing medical knowledge
Martin Chapman
 
PDF
Principles of Health Informatics: Informatics skills - searching and making d...
Martin Chapman
 
PDF
Principles of Health Informatics: Informatics skills - communicating, structu...
Martin Chapman
 
PDF
Principles of Health Informatics: Models, information, and information systems
Martin Chapman
 
PDF
Using AI to understand how preventative interventions can improve the health ...
Martin Chapman
 
Phenoflow: An Architecture for FAIRer Phenotypes
Martin Chapman
 
Generating Computable Phenotype Intersection Metadata Using the Phenoflow Lib...
Martin Chapman
 
Principles of Health Informatics: Artificial intelligence and machine learning
Martin Chapman
 
Principles of Health Informatics: Clinical decision support systems
Martin Chapman
 
Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...
Martin Chapman
 
Technical Validation through Automated Testing
Martin Chapman
 
Scalable architectures for phenotype libraries
Martin Chapman
 
Using AI to understand how preventative interventions can improve the health ...
Martin Chapman
 
Using AI to autonomously identify diseases within groups of patients
Martin Chapman
 
Using AI to understand how preventative interventions can improve the health ...
Martin Chapman
 
Principles of Health Informatics: Evaluating medical software
Martin Chapman
 
Principles of Health Informatics: Usability of medical software
Martin Chapman
 
Principles of Health Informatics: Social networks, telehealth, and mobile health
Martin Chapman
 
Principles of Health Informatics: Communication systems in healthcare
Martin Chapman
 
Principles of Health Informatics: Terminologies and classification systems
Martin Chapman
 
Principles of Health Informatics: Representing medical knowledge
Martin Chapman
 
Principles of Health Informatics: Informatics skills - searching and making d...
Martin Chapman
 
Principles of Health Informatics: Informatics skills - communicating, structu...
Martin Chapman
 
Principles of Health Informatics: Models, information, and information systems
Martin Chapman
 
Using AI to understand how preventative interventions can improve the health ...
Martin Chapman
 
Ad

Recently uploaded (20)

PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
PPTX
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
community health nursing question paper 2.pdf
Prince kumar
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 

Programming in Java: Recursion