Home > Back-end >  I can't understand the flow of multiple recursion
I can't understand the flow of multiple recursion

Time:04-25

I can't expect this code output, I generally don't understand recursion, can you write the flow of this code or explain please, thanks in advance.

static void m(int n) {
 if (n <= 0) {

 } else {
    m(n - 1);
    m(n - 2);
    System.out.println(n);
 }
}

    public static void main(String[] args) {
        m(5);
    }

}

CodePudding user response:

Code is always processed top down (unless you have jump conditions)

For better understanding rewrite your code like this:

public static void main(String[] args) {
{
    m(5);
}

private static int callNumber = 0;

private static int GetCallNumber() {
    return   callNumber;
}

private static void m(int n) {
    int callNumber = GetCallNumber();
    
    System.out.println(callNumber.ToString()   ": static void m("   n.ToString()   ")");
    System.out.println(callNumber.ToString()   ": if ("   n.ToString()   " <= 0)");
    if (n <= 0) {
    } else {
        System.out.println(callNumber.ToString()   ": else");
        System.out.println(callNumber.ToString()   ": m("   n.ToString()   " - 1)");
        m(n - 1);
        System.out.println(callNumber.ToString()   ": m("   n.ToString()   " - 2)");
        m(n - 2);
        System.out.println(callNumber.ToString()   ": System.out.println("   n.ToString()   ")");
        System.out.println(n);
    }

    System.out.println(callNumber.ToString()   ": //exit");
}
1: static void m(5)
1: if (5 <= 0)
1: else
1: m(5 - 1)
2: static void m(4)
2: if (4 <= 0)
2: else
2: m(4 - 1)
3: static void m(3)
3: if (3 <= 0)
3: else
3: m(3 - 1)
4: static void m(2)
4: if (2 <= 0)
4: else
4: m(2 - 1)
5: static void m(1)
5: if (1 <= 0)
5: else
5: m(1 - 1)
6: static void m(0)
6: if (0 <= 0)
6: //exit
5: m(1 - 2)
7: static void m(-1)
7: if (-1 <= 0)
7: //exit
5: System.out.println(1)
1
5: //exit
4: m(2 - 2)
8: static void m(0)
8: if (0 <= 0)
8: //exit
4: System.out.println(2)
2
4: //exit
3: m(3 - 2)
9: static void m(1)
9: if (1 <= 0)
9: else
9: m(1 - 1)
10: static void m(0)
10: if (0 <= 0)
10: //exit
9: m(1 - 2)
11: static void m(-1)
11: if (-1 <= 0)
11: //exit
9: System.out.println(1)
1
9: //exit
3: System.out.println(3)
3
3: //exit
2: m(4 - 2)
12: static void m(2)
12: if (2 <= 0)
12: else
12: m(2 - 1)
13: static void m(1)
13: if (1 <= 0)
13: else
13: m(1 - 1)
14: static void m(0)
14: if (0 <= 0)
14: //exit
13: m(1 - 2)
15: static void m(-1)
15: if (-1 <= 0)
15: //exit
13: System.out.println(1)
1
13: //exit
12: m(2 - 2)
16: static void m(0)
16: if (0 <= 0)
16: //exit
12: System.out.println(2)
2
12: //exit
2: System.out.println(4)
4
2: //exit
1: m(5 - 2)
17: static void m(3)
17: if (3 <= 0)
17: else
17: m(3 - 1)
18: static void m(2)
18: if (2 <= 0)
18: else
18: m(2 - 1)
19: static void m(1)
19: if (1 <= 0)
19: else
19: m(1 - 1)
20: static void m(0)
20: if (0 <= 0)
20: //exit
19: m(1 - 2)
21: static void m(-1)
21: if (-1 <= 0)
21: //exit
19: System.out.println(1)
1
19: //exit
18: m(2 - 2)
22: static void m(0)
22: if (0 <= 0)
22: //exit
18: System.out.println(2)
2
18: //exit
17: m(3 - 2)
23: static void m(1)
23: if (1 <= 0)
23: else
23: m(1 - 1)
24: static void m(0)
24: if (0 <= 0)
24: //exit
23: m(1 - 2)
25: static void m(-1)
25: if (-1 <= 0)
25: //exit
23: System.out.println(1)
1
23: //exit
17: System.out.println(3)
3
17: //exit
1: System.out.println(5)
5
1: //exit
  • Related