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

Recursion Vs Iterative

A recursive method is one that calls itself, directly or indirectly. For recursion to work successfully, each recursive call must simplify the problem and there must be base cases to handle the simplest problems. Recursion involves dividing a problem into smaller pieces, solving the simplest cases and recursively calling itself on the smaller problems until reaching the base cases. This results in repetition through repeated method calls that eventually converge on the base cases to terminate recursion and return the final result.

Uploaded by

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

Recursion Vs Iterative

A recursive method is one that calls itself, directly or indirectly. For recursion to work successfully, each recursive call must simplify the problem and there must be base cases to handle the simplest problems. Recursion involves dividing a problem into smaller pieces, solving the simplest cases and recursively calling itself on the smaller problems until reaching the base cases. This results in repetition through repeated method calls that eventually converge on the base cases to terminate recursion and return the final result.

Uploaded by

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

Arecursivemethodisamethodthatcallsitselfeitherdirectlyor

indirectly
Therearetwokeyrequirementstomakesurethattherecursionissuccessful:
Everyrecursivecallmustsimplifythecomputationinsomeway.
Theremustbespecialcasestohandlethesimplestcomputations.
IterationVs.Recursion
Ifarecursivemethodiscalledwithabasecase,themethodreturnsaresult.
Ifamethodiscalledwithamorecomplexproblem,themethoddividesthe
problemintotwoormoreconceptualpieces:apiecethatthemethodknows
howtodoandaslightlysmallerversionoftheoriginalproblem.Becausethis
newproblemlooksliketheoriginalproblem,themethodlaunchesarecursive
calltoworkonthesmallerproblem.
Forrecursiontoterminate,eachtimetherecursionmethodcallsitselfwitha
slightlysimplerversionoftheoriginalproblem,thesequenceofsmallerand
smallerproblemsmustconvergeonthebasecase.Whenthemethod
recognizesthebasecase,theresultisreturnedtothepreviousmethodcall
andasequenceofreturnsensuresallthewayupthelineuntiltheoriginal
callofthemethodeventuallyreturnsthefinalresult.
Bothiterationandrecursionarebasedonacontrolstructure:Iterationuses
arepetitionstructurerecursionusesaselectionstructure.
Bothiterationandrecursioninvolverepetition:Iterationexplicitlyusesa
repetitionstructurerecursionachievesrepetitionthroughrepeatedmethod
calls.
Iterationandrecursioneachinvolveaterminationtest:Iterationterminates
whentheloopcontinuationconditionfailsrecursionterminateswhenabase
caseisrecognized.
Iterationandrecursioncanoccurinfinitely:Aninfiniteloopoccurswith
iterationiftheloopcontinuationtestneverbecomesfalseinfiniterecursion
occursiftherecursionstepdoesnotreducetheprobleminamannerthat
convergesonthebasecase.
Recursionrepeatedlyinvokesthemechanism,andconsequentlythe
overhead,ofmethodcalls.Thiscanbeexpensiveinbothprocessortimeand
memoryspace.

You might also like