Java Recursive thinking

Source: Internet
Author: User

The so-called recursion, is oneself indirectly or directly to oneself.

Three conditions to be aware of recursion:

1. Boundary conditions

2, recursive forward paragraph

3. Recursive return segment

Recursive progression when boundary conditions are not met

When the boundary condition is met, the recursive return

Here's a small example:

public class Demo

{

public static void Main (String args[])

{

ToBin (6);

}

public static void toBin (int num)

{

if (num>0)

{

ToBin (NUM/2);

System.out.println (num%2);

}

}

}

The first step into the main function calls the Tobin function, 6>0, executes Tobin (3), 3>0, executes Tobin (1), 1>0, executes Tobin (0), the local boundary condition is not satisfied, Therefore executes the print statement in the last called ToBin (1) that satisfies the condition, and the result is that the 1,tobin (1) function ends, returns to the ToBin (3) that called the function, the result is 1, and returns to the ToBin (6) that called this function after the function is finished, and the result is 0, The final answer is 1,1,0.

This article is from the Java recursion blog, so be sure to keep this source http://9691015.blog.51cto.com/9681015/1589792

Java Recursive thinking

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.