Está en la página 1de 22

Recursion vs.

Iteration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program

that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o

mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in

both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con

tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite

ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev

es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic

ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite

ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program

that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o

mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in

both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con

tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite

ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev

es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic

ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite

ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program

that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o

mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in

both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con

tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite

ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3 Recursion vs. Ite
ration
In the previous sections, we studied two functions that can be implemented eithe
r recursively or iteratively. In this section, we compare the two approaches and
discuss why the programmer might choose one approach over the other in a partic
ular situation. Both iteration and recursion are based on a control structure: I
teration uses a repetition structure (such as for and while); recursion uses a s
election structure (such as if and if/else). Both iteration and recursion involv
e repetition: Iteration explicitly uses a repetition structure; recursion achiev
es repetition through repeated function calls. Iteration and recursion both invo
lve a termination test: Iteration terminates when the loop-continuation conditio
n fails; recursion terminates when a base case is recognized. Iteration with cou
ntercontrolled repetition and recursion each gradually approach termination: Ite
ration keeps modifying a counter until the counter assumes a value that makes th
e loop-continuation condition fail; recursion keeps producing simpler versions o
f the original problem until the base case is reached. Both iteration and recurs
ion can occur infinitely: An infinite loop occurs with iteration if the loop-con
tinuation test never becomes false; infinite recursion occurs if the recursion s
tep does not reduce the problem each time in a manner that converges on the base
case. Recursion has many negatives. It repeatedly invokes the mechanism and, co
nsequently, the overhead of function calls. This repetition can be expensive in
both processor time and memory space. Each recursive call causes another copy of
the function (actually only the function s variables) to be created; this set of
copies can consume considerable memory. Iteration normally occurs within a funct
ion, so the overhead of repeated function calls and extra memory assignment is o
mitted. So why choose recursion? Software Engineering Observation 4.8 Any proble
m that can be solved recursively can also be solved iteratively (nonrecursively)
. A recursive approach normally is preferred over an iterative approach when the
recursive approach more naturally mirrors the problem and results in a program
that is easier to understand and debug. Often, a recursive approach can be imple
mented with few lines of code when a corresponding iterative approach may take l
arge amounts of code. Another reason to choose a recursive solution is that an i
terative solution may not be apparent. 4.8 Performance Tip 4.3

También podría gustarte