A powerful, extremely simple recursive small example that helps you understand recursion from the perspective of program execution

Source: Internet
Author: User

1  Public classTest {2     Public Static voidMain (string[] args) {3Swap3);4    }5     Public Static intI=1;//Global Variables6     Public Static voidSwapintA) {//Local Variables7       if(a>0){8a--;9System. out. println ("Section"+i+"Layer:");TenSystem. out. println ("A-1:"+a); Onei++; A       } -       Else{ -System. out. println ("---Gorgeous split-line---"); the       return; -       } - swap (a); -i--; +System. out. println ("Section"+i+"Layer:"); -System. out. println ("A:"+a); +    } A}

Program output:

1th Floor:
A-1:2
2nd Floor:
A-1:1
3rd Floor:
A-1:0
---Gorgeous split-line---
3rd Floor:
a:0
2nd Floor:
A:1
1th Floor:
A:2

The program has been posted, the implementation of the function is very simple, is recursive to a number a minus one, until a==0.

Here are the points to note:

(1) is the difference between the global variable I in the program and the local variable A. A global variable is the same value that runs through the recursive layers, and each layer operates on it. But local variables are, each layer has its own copy value, passed to the next layer is the upper copy value, when a layer is returned, this layer of local variables will be popped from the stack, destroyed. Therefore, we should never use the return of local variables when designing recursive programs to get the changes caused by the next layer of operations to local variables, because they are not returned. See diagram below for details.

(2) Return in line 15, this code is necessary, cannot default program does not conform to the recursive condition, will return itself. If you do not write this sentence here, when A==0, the program will skip the 7~12 line, directly execute 17 lines, repeated recursion, causing the stack overflow.

(3) When the program returns to the previous level, it will continue execution at the next level when it enters the recursion, in this procedure, return; after execution (the last return; except), the 18th line i--will be executed immediately;

The following is a detailed procedure when the program executes:

The program executes from Arrow 1 and executes the code sequentially in the order of arrows 1 through 8. Each time in swap (a), go to the next layer, and then execute i--each time it is returned by the next layer; this line of code.

The focus is on the two notes at Arrows 2 and 6.

A powerful, extremely simple recursive small example that helps you understand recursion from the perspective of program execution

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.