C + + iterative and recursive analysis

Source: Internet
Author: User

"What is recursion"

Recursion is called repeatedly by the function itself.

There are two points to note when using recursion:

1) recursion is the invocation of itself within a procedure or function;

2) When using recursion, there must be a definite recursive end condition called a recursive exit.

Recursion is divided into two stages:

1) Recursion: The solution of the complex problem is pushed to a simpler problem than the original problem;

2) Regression: When the simplest case is obtained, it is gradually returned and the complex solution is obtained in turn.


"What is Iteration"

Use the original value of the variable to derive a new value for the variable. If recursion calls itself by itself, the iteration is a non-stop call to B.

"The difference between recursion and iteration"

Recursion and function call itself; iteration is a loop, and there is a self-change in the loop body.


"Code parsing"

This is recursive  int Funca (int n)  {      if (n > 1)         return N+funca (n-1);      else          return 1;  }  This is the iteration  int FUNCB (int n)  {      int i,s=0;      for (i=1;i<n;i++)         s+=i;      return s;  }
here recursion, that is, Funa itself calls; The iteration here, that is, the FUNB constant cyclic invocation of s, makes s self-changing;

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + iterative and recursive analysis

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.