Results 1 to 8 of 8

Thread: Iteration v. Recursion...

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Talking 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

  2. #2
    Recursion: a function calling itself
    Iteration: a loop such as For, While, etc.

  3. #3
    Megatron
    Guest
    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)

  4. #4

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Thumbs up

    Thanks

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    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.

  6. #6
    Zaei
    Guest
    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);
    }

  7. #7
    Heh, I told you there'd be bugs. But the basic concept is there.

  8. #8
    Zaei
    Guest
    Quite =).

    Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width