Results 1 to 3 of 3

Thread: [RESOLVED] recursive vs iteration

  1. #1

    Thread Starter
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Resolved [RESOLVED] recursive vs iteration

    Hi,

    Anyone can explain what is the difference of two?

    Thanks,

    Popskie

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: recursive vs iteration

    Both involve performing the same operation over and over, but when iterating you don't start the next operation until the previous has finished. Recursion involves starting the next operation within the previous. That means that the first operation doesn't finish until the second has finished, which doesn't finish until the third has finished, etc. Recursion is good for certain situations but because of the aforementioned situation you can stretch your system resources during a long running operation. Run this code as an example of each and the difference:
    Code:
    private void Form1_Load(object sender, EventArgs e)
    {
        // Iteration.
        for (int i = 0; i < 5; i++)
        {
            MessageBox.Show("Start iteration: " + i.ToString());
            MessageBox.Show("End iteration: " + i.ToString());
        }
    
        // Recursion.
        this.DoSomething(0);
    }
    
    private void DoSomething(int i)
    {
        MessageBox.Show("Start recursion: " + i.ToString());
    
        if (i < 4)
        {
            this.DoSomething(i + 1);
        }
    
        MessageBox.Show("End recursion: " + i.ToString());
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

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