Why do I get a missing return statement error although there is a return statement in each of my if and if else blocks?

RenukaA

What I am trying to do here out of flower1 and flower2, one is even and one is odd then return true. If both are even, return false. If both are odd, return false. When my code is:

public class OppositesAttract {

  public static boolean isLove(final int flower1, final int flower2) {
   
    if(flower1%2==0 && flower2%2==0){
      return false;
    }else
    if(flower1%2!=0 && flower2%2!=0){
      return false;
    } else
    if(flower1%2==0 || flower2%2==0){
      return true;
    }

  }
  
}

I get a "missing return statement" error. So I added:

public class OppositesAttract {

  public static boolean isLove(final int flower1, final int flower2) {
   
    if(flower1%2==0 && flower2%2==0){
      return false;
    }else
    if(flower1%2!=0 && flower2%2!=0){
      return false;
    } else
    if(flower1%2==0 || flower2%2==0){
      return true;
    }else{
      return true;
    }

  }
  
}

Now, the code works but I do not understand why I have to add the additional return statement.

Joeblade

The compiler doesn't know the first 3 terms cover all situations.

if(flower1%2==0 && flower2%2==0){
  return false;
} else if(flower1%2!=0 && flower2%2!=0){
  return false;
} else if(flower1%2==0 || flower2%2==0){
  return true;
} 

to you this reads as: all options are covered. but the compiler just sees:

if (somethingThatMayBeTrue) {

} else if (somethingElseThatMayBeTrue) {

} else if (aThirdThingThatMayBeTrue) {

} .... and what if none of them are?

You may know that the last else if should always be true (since you know they are not both uneven) but the compiler doesn't generally try to understand your code.

in your case, the last clause (aThirdThingThatMayBeTrue, flower1%2==0 || flower2%2==0) is actually (somethingThatIsAlwaysTrueIfPreviousTermsAreFalse).

so you can treat it as such:

if(flower1%2==0 && flower2%2==0){
  return false; 
} else if(flower1%2!=0 && flower2%2!=0){
  return false; 
} else {
  return true; 
} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I solve this missing return statement error?

"Missing return statement" error in Java

Getting Error: return statement is missing in switch statement

Why if-else if-else (within a loop) in method gives "return statement missing" while all if-else branches have a return statement?

I get the error "Unreachable statement" return in android

Why does my if else statement return the incorrect result?

Missing return statement after if-else

Why is my if statement not returning the return statement?

missing return statement when I run my app in Android Studio

Why do we not need "else" in an if-statement if it contains "return"?

Why does this code have a missing return statement error?

Why does throwing an unchecked exception removes the "missing return statement" error

Why do I get "Expected Statement" error even though I ended my if statement?

If else statement inside return " " statement

Why do I get this "unreachable statement" error?

"Missing return statement" after switch(enum) - Why?

else statement or return in ruby

Return if-else statement

Missing return statement with switch

Missing return statement in PhpStorm

Missing return statement in for loop

PHPStorm missing return statement

Missing return statement

AsyncTask missing return statement

Missing return statement with for loop

Missing a return statement somewhere?

How to avoid compilation error: missing return statement

./ucgenAlanı.java:23: error: missing return statement }

Java Compiler Error: Missing Return Statement