mercredi 13 décembre 2017

In case of nested loops, what happens in case of multiple print outputs?

public class Test {
    public static void main(String[] args) {
        print(4);
    }

    static void print(int n){
        if (n <= 1)
            System.out.print("A");
        else {
            print(n-1);
            print(n-2);
        }
    }
}

The output result of this code is AAAAA. But I want to know how? When do I consider (n-1) and when (n-2)?

Aucun commentaire:

Enregistrer un commentaire