|
-
Nov 4th, 2001, 05:46 PM
#1
Iteration v. Recursion...
Can some explain the difference between the two?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Nov 4th, 2001, 06:13 PM
#2
Member
Recursion: a function calling itself
Iteration: a loop such as For, While, etc.
-
Nov 4th, 2001, 06:15 PM
#3
They're two completely different things. Recrusion is when a function calls itself, and iteration is one repetition of a sequence of instructions/events (e.g. in a loop, one iteration is once through the code in the loop)
-
Nov 4th, 2001, 06:30 PM
#4
Thanks
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Nov 4th, 2001, 07:02 PM
#5
Member
Originally posted by filburt1
Recursion: a function calling itself
Iteration: a loop such as For, While, etc.
Examples:
Code:
// recursion:
int stuff(int x)
{
if (x == 0) return 0;
else return stuff(x - 1);
}
// iteration
int someValue = 0;
for (int i = 0; i < x, i++)
{
someValue += i
}
Both return (except for the billions of bugs I didn't notice ) the sum from 0 to x.
-
Nov 4th, 2001, 07:27 PM
#6
The recursion example looks like it returns 0 =).
Code:
stuff(4) returns 0
return (4-1) returns 0 ^
return (3-1) returns 0 ^
return (2-1) returns 0 ^
return (1-1) -> returns 0 ^
Z.
[edit]
It does, the code should look like:
Code:
int stuff(int x) {
if(x == 0) return 0;
else return x + stuff(x-1);
}
-
Nov 4th, 2001, 07:37 PM
#7
-
Nov 4th, 2001, 09:14 PM
#8
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|