Home > front end >  Can someone explain the logic of recursion in this pattern printing method?
Can someone explain the logic of recursion in this pattern printing method?

Time:02-25

I'm trying to piece together the logic in this example. This is the normal code. FYI, I had a university tutor help me with this example but he's still trying to figure out how to explain it to me. The code is also done, this is just for my own knowledge of recursion.

public class Practice {

    public static void printTriangle(int n) { 

        if (n < 1) { 
            return; 
            
        } 
        else {
            
        printTriangle(n - 1); 
        printStars(n); 
        System.out.println();
        }
    } 
    public static void printStars(int n) {
        if (n > 0) {
        System.out.print("*");
        printStars(n-1);
        }
    }
    public static void main(String[] args) {
        printTriangle(5);
    }
}
 Output
*
**
***
****
*****

This is the test code with markers that I used to find out what is happening when. It's kind of an eyesore so if you think you can explain it without looking at this mess, by all means.

public class Practice {

    public static void printTriangle(int n) { 

        if (n < 1) { 
            System.out.println(n);
            
            return; 
            
        } 
        else {
        System.out.println("1");
        printTriangle(n - 1); 
        System.out.println(n);
        printStars(n);
        System.out.println("A");
        }
    } 
    public static void printStars(int n) {
        if (n > 0) {
        System.out.print(n);
        System.out.print("*");
        printStars(n-1);
        }
    }
    public static void main(String[] args) {
        printTriangle(5);
    }
}
1
1
1
1
1
0
1
1*A
2
2*1*A
3
3*2*1*A
4
4*3*2*1*A
5
5*4*3*2*1*A

I've figured it out up until the first star is printed. I printed the value of n after the printTriangle method decremented it to 0, then I printed the value of n right before the first star is printed and somehow, it's equal to 1. I'm just curious as to how the value of n is increasing. The value goes into the printStars method at 1 and also keeps increasing as it prints stars

CodePudding user response:

I will hopefully answer your question by providing both a written and visual explanation:

Written:

First we start with printTriangle(5). Since n < 1 is false, we continue by doing printTriangle(4). This process continues until we reach printTriangle(0). Since printTriangle(0) will not output anything (since 0 < 1 is true), we start with printTriangle(1):

From printTriangle(1) we go to printStars(1). We can only print one star from here. After we do this, we print a newline. Next, we have printTriangle(2) which leads us into printStars(2). This gives 1 star, but leads into printStars(1) which gives us a second star and a newline. This process continues until we reach our 5 stars.

Visual Representation:

enter image description here

I hope this helped answer your question! Please let me know if you need further clarification or details :)

CodePudding user response:

Well,

printTriangle(5) calls printTriangle(4)
printTriangle(4) calls printTriangle(3)
printTriangle(3) calls printTriangle(2)
printTriangle(2) calls printTriangle(1) printTriangle(1) calls printTriangle(0)
printTriangle(0) does nothing, but returns to printTriangle(1)

printTriangle(1) procedes with calling printStars(1)
printStars(1) prints a star and calls printStars(0)
printStars(0) does nothing and returns to printStars(1)
printStars(1) has nothing more to do and returns to printTriangle(1)
printTriangle(1) prints a linebreak and returns to printTriangle(2)

printTriangle(2) procedes with calling printStars(2)
printStars(2) prints a star and calls printStars(1)
printStars(1) prints a star and calls printStars(0)
printStars(0) does nothing and returns to printStars(1)
printStars(1) has nothing more to do and returns to printStars(2)
printStars(2) has nothing more to do and returns to printTriangle(2)
printTriangle(2) prints a linebreak and returns to printTriangle(3)

...

  • Related