Recursion and iteration

Source: Internet
Author: User
Recursion and iteration are both based on the control structure: iteration uses a repetitive structure, while recursion uses a Selective structure. Recurrence and iteration involve repetition: iterations explicitly use repetition structures, while recursion repeats through repeated function calls. Recursion and iteration both involve the termination test: iteration ends when the loop condition fails, and Recursion ends when the basic situation occurs. Using counters to control repeated iterations and Recursion gradually reach the end point: the iteration keeps modifying the counter until the counter value makes the loop condition fail; recursion continuously produces a simplified copy of the initial problem, until the basic information is reached. Iteration and Recursion can be performed infinitely: if the test of the cyclic condition never turns to false, iteration will have an infinite loop. If recursion can never be pushed back to the basic situation, infinite recursion will occur.
Recursion has many disadvantages. It repeats the call mechanism, so the overhead of repeated function calls is large, which takes a long processor time and a large amount of memory space. Every recursive call generates another copy of the function (actually only another copy of the function variable), which consumes a lot of memory space. Iterations usually occur in functions, so there is no overhead for repeatedly calling functions and assigning values to excess memory. So why do we choose recursion?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.