Java return does weird things

Viewed 245

I have just started learning Java for university with some excercises. I am not new to programming, only to Java. I have followed all instrucions, but Java's return function does some really weird things. I have inserted some debug outputs to better understand the code. CMD.output of the program is included.

I thought that the function would terminate directly after return. But why does it not do in this case?

public class Rekursion {

    public static void main(String[] args) {
        int zahl = 10;
        System.out.println("debugStart");
        boolean even = isEven(zahl);
        System.out.println("debugEnd");

        if (even == true) {
            System.out.println(zahl + " is even");
        } else {
            System.out.println(zahl + " is uneven");
        }
    }

    public static boolean isEven(int n) {
        System.out.println(n);
        if (n > 1) {
            System.out.println("debugx");
            isEven(n - 2);
        }

        if (n == 0) {
            System.out.println("debug1");
            return true;
        } else if (n == 1) {
            System.out.println("debug2");
            return false;
        } else {
            System.out.println("ERROR");
            return false;
        }
    }
}

Output (cmd):

debugStart
10
debugx
8
debugx
6
debugx
4
debugx
2
debugx
0
debug1

Program needs to stop here, normally. But it continues....

ERROR
ERROR
ERROR
ERROR
ERROR
debugEnd
10 is uneven
4 Answers

You should return the recursion call too,

 if(n>1){
     System.out.println("debugx");
     return isEven(n-2);//return at this point
 }

As you haven't returned, your recursive calls are reaching else statements hence returning false.

That's because you have a recursion inside the method

 if(n>1){
        System.out.println("debugx");
        isEven(n-2);
 }

How it works

  1. 10 > 1, go inside and 10-2 = 8
  2. 8 > 1 ,go inside and 8-2 = 6
  3. 6 > 1, go inside and 6-2 = 4
  4. 4 > 1, go inside and 4-2 = 2
  5. 2 > 1, go inside and 2-2 = 0
  6. 2 > 1, no , then n===0,yes -> System.out.println("debug1");

You need to add a return in isEven(n-2)

Corrected code :

public class Rekursion{

public static void main(String[] args){
    int zahl = 10;
    System.out.println("debugStart");
    boolean even = isEven(zahl);
    System.out.println("debugEnd");

    if(even == true){
        System.out.println(zahl+" is even");
    }

    else{
        System.out.println(zahl+" is uneven");
    }
}

public static boolean isEven(int n){
    System.out.println(n);
    if(n>1){
        System.out.println("debugx");
        return isEven(n-2);
    }

    if (n == 0) {
        System.out.println("debug1");
        return true;
    }

    else if (n == 1) {
        System.out.println("debug2");
        return false;
    }

    else{
        System.out.println("ERROR");
        return false;
    }
}

}

Added return statement.

I'm pretty sure you just forgot to add the return statement before isEven(n-2).

So the corrected code of the isEven() method would be like this:

public static boolean isEven(int n){
    System.out.println(n);
    if(n>1){
        System.out.println("debugx");
        return isEven(n-2);
    }

    if (n == 0) {
        System.out.println("debug1");
        return true;
    }

    else if (n == 1) {
        System.out.println("debug2");
        return false;
    }

    else{
        System.out.println("ERROR");
        return false;
    }
}

The output should now look like this:

debugStart
10
debugx
8
debugx
6
debugx
4
debugx
2
debugx
0
debug1
debugEnd
10 is even
Related