Recursion:

  • The idea of a method running itself inside of itself.
int n = 5;
public static int product(int n){
    if (n <= 1){
        return 1;
    }
    else {
        return n * product(n-2);
    }
}
//iteration 1: product(int 5) = 5 * product(3) = 15;
//iteration 2: product(int 3) = 3 * product(1) = 3;
//iteration 3: product(int 1) = 1;

System.out.println(product(5));
15
public static int f(int n){
    if (n == 0){
        return 0;
    }

    else if (n == 1){
        return 1;
    }

    else {
        return f(n-1) + f(n-2);
    }

    //iteration 1: f(5) = f(4) + f(3);
    //iteration 2: f(4) = f(3) + f(2);
    //f(3) = f(2) + f(1) or f(3) = f(2) + 1;
    //f(2) = f(1) + f(0);
    //f(2) = 1 + 0 = 1;


    //reversing back...
    //f(3) = 1 + 1 = 2;
    //f(4) = 2 + 1 = 3;
    //f(5) = 3 + 2 = 5;
}

System.out.println(f(5));
5