How does the recursion method work?

I found this recursion example online, but I do not understand what is going on there.

public class MysteryClass { public static void mystery(int n) { if (n > 0) { mystery(n-1); System.out.println(n * 4); mystery(n-1); } } public static void main(String[] args) { MysteryClass.mystery(2); } } 

output

 4 8 4 

I understand that

  • 2 over 0
  • 2 - 1 = 1
  • 1 is greater than 0
  • 1 - 1 = 0
  • 0 not more than 0
  • Now we go to this line: System.out.println(n * 4);
  • 1 * 4 = 4
  • 2 * 4 = 8
  • Here I do not understand why I get another "4" output

What happens in step 9?

I used a debugger but still don't understand.

+5
source share
7 answers
 MysteryClass.mystery(2); if (2 > 0) (true) ---> mystery(2-1); | if(1 > 0) (true) ---> mystery (1-1); // Action (A) | | if(0 > 0) (false) ---> // do nothing | | | System.out.println(1 * 4); | mystery(1-1); | System.out.println(2 * 4); mystery(2-1); ---> // repeat action (A) 
+13
source

If you run this code with n=2

  mystery(n-1); System.out.println(n * 4); mystery(n-1); 

This means that it works completely secretly (1) twice. First, it starts secret (1), which gets output 4, but the next two recursions with secret (0) end the veins. Therefore he continues

 System.out.println(n * 4); 

What is 8

Then he re-launches the mystery (1), which prints 4.

+5
source

I can only understand recursion by thinking about what happens if I copy and paste all the code so that everything is in one place.

You can replace mystery(2) with three lines

 mystery(1); System.out.println(8); mystery(1); 

Therefore, your main method may also say

 public static void main(String[] args) { mystery(1); System.out.println(8); mystery(1); } 

You can replace both of these mystery(1); calls mystery(1); on the

 mystery(0); System.out.println(4); mystery(0); 

This means that the main method can also say

 public static void main(String[] args) { mystery(0); System.out.println(4); mystery(0); System.out.println(8); mystery(0); System.out.println(4); mystery(0); } 

mystery(0); does nothing, so main can just say

 public static void main(String[] args) { System.out.println(4); System.out.println(8); System.out.println(4); } 
+5
source

You do double recursion - the same function is called TWICE. Thus, your execution path is incorrect. For clarity, note the following change:

  mystery(n-1); // pretend this is function "A" System.out.println(n * 4); mystery(n-1); // pretend this is function "B" 
  • mystery (2) // start recursion
  • A (2-1) โ†’ A (1) // n> 0, so we call A ()
  • A (1-1) โ†’ A (0) // n == 0, so nothing happens
  • return from (3)
  • println (1 * 4) โ†’ 4
  • return from (2)
  • println (n * 4) โ†’ println (2 * 4) โ†’ 8
  • B (2-1) โ†’ B (1) // we will return to the original (1) challenge of secrecy
  • A (1-0) โ†’ A (0) // etc ...
+4
source

here I do not understand why I get 4 again as a conclusion of what happens in step 9 ???

Because n does not change. That way, you run it again with mystery(2-1);

+3
source

If you replace n with 2, you will see:

 if (2 > 0) { mystery(2-1); // prints 4 System.out.println(2 * 4); mystery(2-1); // prints 4 } 

You have two calls with mystery(2-1); One before and one after System.out.println

Since all mystery(2-1); will be the result of printing 4, which means the result is 4 8 4

+2
source

To understand the secret (n-1), to understand the secret:

 int n1 = 2 if (n1 > 0){ // step 1 int n2 = n1 - 1; // step2: 1 if (n2 > 0){ // step 3 int n3 = n2 - 1; // step4: 0 if (n3 > 0){ // step 5: false ... } System.out.println(n2 * 4); // step 6: print 4 int n3_1 = n2 - 1; // step 7: 0 if (n3_1 > 0){ // false ... } } System.out.println(n1 * 4); // step 8: print 8 if (n2 > 0){ int n3 = n2 - 1; // 0 if (n3 > 0){ // false ... } System.out.println(n2 * 4); // step 9: print 4 int n3_1 = n2 - 1; // 0 if (n3_1 > 0){ // false ... } } } } 
+1
source

Source: https://habr.com/ru/post/1237110/


All Articles